簡體   English   中英

從圖庫中選擇時裁剪的圖像,但從相機單擊時未裁剪

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

我知道這個問題是 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

這應該可以完成工作!

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