简体   繁体   English

从图库中选择时裁剪的图像,但从相机单击时未裁剪

[英]Image cropped when picked from gallery but not cropped when clicked from camera

I know this question is copy of other questions on SO community but i am re-asking it as i have some different code and unable to figure out solution for my problem.我知道这个问题是 SO 社区上其他问题的副本,但我正在重新提问,因为我有一些不同的代码并且无法找出我的问题的解决方案。 My code is working(means going to crop intent) when i take image from gallery but when i try to click image it never crops image and try to run code which should be run after crop.当我从图库中获取图像时,我的代码正在工作(意味着要裁剪意图),但是当我尝试单击图像时,它从不裁剪图像并尝试运行应在裁剪后运行的代码。 Here is my code:这是我的代码:

@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();
    }
}

Please point me to right direction as i am noob with android development.请指出我正确的方向,因为我是 android 开发的菜鸟。 Thanks谢谢

EDIT after answer suggested by @johnrao07 i get following error:在@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

This should do the work!这应该可以完成工作!

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();
    }
}
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.
    } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM