【发布时间】:2015-06-22 05:08:45
【问题描述】:
概述:我有一个简单的布局,带有一个按钮,当按下它时.. 打开画廊告诉我选择要裁剪的图片,一旦选择了图片,它就会进入裁剪图像屏幕。一旦图像裁剪完成并点击“保存/完成”,它应该会在我的 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();
}
}
下面是第二个活动,它允许我们进入画廊并选择一张图片,然后将其返回到第一个活动进行裁剪:
package com.example.pau.crop_test;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
public class GalleryUtil extends Activity{
private final static int RESULT_SELECT_IMAGE = 100;
public static final int MEDIA_TYPE_IMAGE = 1;
private static final String TAG = "GalleryUtil";
String mCurrentPhotoPath;
File photoFile = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
//Pick Image From Gallery
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_SELECT_IMAGE);
}catch(Exception e){
e.printStackTrace();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode){
case RESULT_SELECT_IMAGE:
if (resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
try{
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
//return Image Path to the Main Activity
Intent returnFromGalleryIntent = new Intent();
returnFromGalleryIntent.putExtra("picturePath",picturePath);
setResult(RESULT_OK,returnFromGalleryIntent);
finish();
}catch(Exception e){
e.printStackTrace();
Intent returnFromGalleryIntent = new Intent();
setResult(RESULT_CANCELED, returnFromGalleryIntent);
finish();
}
}else{
Log.i(TAG,"RESULT_CANCELED");
Intent returnFromGalleryIntent = new Intent();
setResult(RESULT_CANCELED, returnFromGalleryIntent);
finish();
}
break;
}
}
}
下面是显示按钮和图像视图的布局:
<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>
【问题讨论】:
-
包括与问题无关的部分(所谓的绒毛或闲聊),会增加在文本中出现拼写错误的机会(环境。!)把它们排除在外。