2
概述:我有一個簡單的佈局,其中有一個按鈕,當按下..打開畫廊告訴我選擇一張圖片裁剪,一旦圖片被選中,它會進入裁剪圖像屏幕。一旦圖像裁剪完成,我點擊「保存/完成」它應該顯示新的裁剪圖像到我的ImageView。在ImageView上顯示剪裁的圖像
問題:我似乎無法將新的裁剪圖像顯示到ImageView;當我在完成剪裁圖像後點擊「保存」時,它會返回到我的主佈局,表明它已保存,但從未將圖像顯示在視圖上。
當按鈕按下並進入第二個活動時,此活動啓動。一旦獲得第二個活動的圖片,它就會裁剪圖像並將其顯示在ImageView上。
package com.example.pau.crop_test;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.File;
public class ImageSelecter extends Activity {
private final int GALLERY_ACTIVITY_CODE = 200;
private final int RESULT_CROP = 400;
private ImageView imageView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_crop);
ImageView imageView2 = (ImageView) findViewById(R.id.imageView2);
Button profile_button = (Button) findViewById(R.id.button);
profile_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Start Activity To Select Image From Gallery
Intent gallery_Intent = new Intent(getApplicationContext(), GalleryUtil.class);
startActivityForResult(gallery_Intent, GALLERY_ACTIVITY_CODE);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_ACTIVITY_CODE) {
if (resultCode == Activity.RESULT_OK) {
String picturePath = data.getStringExtra("picturePath");
//perform Crop on the Image Selected from Gallery
performCrop(picturePath);
}
}
if (requestCode == RESULT_CROP) {
if (resultCode == Activity.RESULT_OK) {
if (data != null) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap selectedBitmap = extras.getParcelable("data");
// Set The Bitmap Data To ImageView
ImageView image =(ImageView) findViewById(R.id.imageView2);
image.setImageBitmap(selectedBitmap);
// imageView2.setScaleType(ImageView.ScaleType.FIT_XY);
}
}
}
}
}
private void performCrop(String picUri) {
try {
//Start Crop Activity
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
File f = new File(picUri);
Uri contentUri = Uri.fromFile(f);
cropIntent.setDataAndType(contentUri, "image/*");
// set crop properties
cropIntent.putExtra("crop", "true");
// indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
// indicate output X and Y
cropIntent.putExtra("outputX", 280);
cropIntent.putExtra("outputY", 280);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, RESULT_CROP);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
// display an error message
String errorMessage = "your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
下面是次活動,讓我們進入我們的畫廊和選擇照片,然後將其返回到第一個活動進行裁剪:
下面是我的佈局,顯示按鈕,圖像視圖:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
>
<ImageView
android:layout_width="200dp"
android:layout_height="fill_parent"
android:id="@+id/imageView2"
android:layout_marginTop="41dp"
android:contentDescription="@string/abc_activity_chooser_view_see_all" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/crop"
android:id="@+id/button"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/imageView2"
android:layout_toEndOf="@+id/imageView2" />
</RelativeLayout>
包括非問題相關部分(所謂的絨毛或閒聊) ,增加了在文本中出現拼寫錯誤的機率(environtment!!),只是將它們排除在外。 – Anthon