2015-06-22 133 views
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> 
+0

包括非问题相关部分(所谓的绒毛或闲聊) ,增加了在文本中出现拼写错误的几率(environtment!!),只是将它们排除在外。 – Anthon

回答

0

把一些调试在requestCode == RESULT_CROP块,以确保部是奔跑G。如果ImageView没有得到更新,并且由于唯一进行更新的地方是成功的作物,那么它将暗示活动没有如您期望的那样返回