从图库中拾取时图像被裁剪,但从相机中单击时不会裁剪

时间:2015-11-24 12:02:47

标签: android image android-intent crop

我知道这个问题是关于SO社区的其他问题的副本,但我重新问它,因为我有一些不同的代码,无法找到我的问题的解决方案。当我从图库中获取图像时,我的代码正在工作(意味着转向裁剪意图)但是当我尝试单击图像时,它从不裁剪图像并尝试运行应该在裁剪后运行的代码。这是我的代码:

@Override
public boolean onContextItemSelected(MenuItem item)
{
    switch (item.getItemId())
    {
        case R.id.take_photo:
            //Toast.makeText(context, "Selected Take Photo", Toast.LENGTH_SHORT).show();
            takePhoto();
            break;

        case R.id.choose_gallery:
            //Toast.makeText(context, "Selected Gallery", Toast.LENGTH_SHORT).show();
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, 1);
            break;

        case R.id.share_cancel:
            closeContextMenu();
            break;
        default:
            return super.onContextItemSelected(item);
    }
    return true;
}
public void takePhoto()
{
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    intent.putExtra("crop", "true");
    File folder = new File(Environment.getExternalStorageDirectory() + "/LoadImg");
    if(!folder.exists()) {
        folder.mkdir();
    }        
    final Calendar c = Calendar.getInstance();
    String new_Date= c.get(Calendar.DAY_OF_MONTH)+"-"+((c.get(Calendar.MONTH))+1)   +"-"+c.get(Calendar.YEAR) +" " + c.get(Calendar.HOUR) + "-" + c.get(Calendar.MINUTE)+ "-"+ c.get(Calendar.SECOND);
    path=String.format(Environment.getExternalStorageDirectory() +"/LoadImg/%s.png","LoadImg("+new_Date+")");
    File photo = new File(path);
    intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photo));
    startActivityForResult(intent, 2);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==1) {
        photoUri=data.getData();
        if (photoUri != null) {
            crop(photoUri);
            //  new GetImages().execute();
        }
    }

    if(requestCode==2) {
        Log.v("Load Image", "Camera File Path=====>>>"+path);
        image_list.add(path);
        Log.v("Load Image", "Image List Size=====>>>"+image_list.size());
        //updateImageTable();
        //new GetImages().execute();
        crop(photoUri);
    }
    if (requestCode == 3) {
        Bundle extras = data.getExtras();
        String selectedImagePath = mCropImagedUri.getPath();

        Log.i("TAG", "After Crop selectedImagePath " + selectedImagePath);

        if (extras != null) {

            Log.i("TAG", "Inside Extra " + selectedImagePath);
            Bitmap photo = (Bitmap) extras.get("data");


            Log.i("TAG", "new selectedImagePath before file "
            + selectedImagePath);
            Log.i("TAG", "After File Created  " + selectedImagePath);
            image_list.add(selectedImagePath);
        }
        new GetImages().execute();
    }
}

请指出正确的方向,因为我是Android开发的菜鸟。感谢

修改 在@ johnrao07建议回答后,我得到以下错误:

11-24 17:16:35.828: E/AndroidRuntime(8399): FATAL EXCEPTION: main
11-24 17:16:35.828: E/AndroidRuntime(8399): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=0, data=null} to activity {com.example.camera/com.example.camera.MainActivity}: java.lang.NullPointerException
11-24 17:16:35.828: E/AndroidRuntime(8399):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3141)
11-24 17:16:35.828: E/AndroidRuntime(8399):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3184)
11-24 17:16:35.828: E/AndroidRuntime(8399):     at android.app.ActivityThread.access$1100(ActivityThread.java:130)
11-24 17:16:35.828: E/AndroidRuntime(8399):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
11-24 17:16:35.828: E/AndroidRuntime(8399):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-24 17:16:35.828: E/AndroidRuntime(8399):     at android.os.Looper.loop(Looper.java:137)
11-24 17:16:35.828: E/AndroidRuntime(8399):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-24 17:16:35.828: E/AndroidRuntime(8399):     at java.lang.reflect.Method.invokeNative(Native Method)
11-24 17:16:35.828: E/AndroidRuntime(8399):     at java.lang.reflect.Method.invoke(Method.java:511)
11-24 17:16:35.828: E/AndroidRuntime(8399):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-24 17:16:35.828: E/AndroidRuntime(8399):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-24 17:16:35.828: E/AndroidRuntime(8399):     at dalvik.system.NativeStart.main(Native Method)
11-24 17:16:35.828: E/AndroidRuntime(8399): Caused by: java.lang.NullPointerException
11-24 17:16:35.828: E/AndroidRuntime(8399):     at com.example.camera.MainActivity.onActivityResult(MainActivity.java:263)
11-24 17:16:35.828: E/AndroidRuntime(8399):     at android.app.Activity.dispatchActivityResult(Activity.java:5192)
11-24 17:16:35.828: E/AndroidRuntime(8399):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3137)
11-24 17:16:35.828: E/AndroidRuntime(8399):     ... 11 more

2 个答案:

答案 0 :(得分:1)

这应该做的工作!

if(requestCode==2) {
    Log.v("Load Image", "Camera File Path=====>>>"+path);
    image_list.add(path);
    photoUri=data.getData();
    if (photoUri != null) {
        crop(photoUri);
        //  new GetImages().execute();
    }
}

答案 1 :(得分:1)

if(requestCode==2) {
        Log.v("Load Image", "Camera File Path=====>>>"+path);
        image_list.add(path); 
        Log.v("Load Image", "Image List Size=====>>>"+image_list.size());
        //updateImageTable(); 
        //new GetImages().execute(); 
        //crop(photoUri); **// THE VALUE OF photoUri is null here.**
photoUri = Uri.parse(path);
        crop(photoUri); // THIS MAY BE WORK FOR YOU.
    }