来自画廊的裁剪图像崩溃应用程序 - Android

时间:2014-11-14 04:53:51

标签: android imageview crop

我有一个代码可以从图库中裁剪图像,或者用相机拍摄一张图像,然后裁剪它。当我用相机拍照然后裁剪它时,我的代码有效,但如果我选择从画廊裁剪一个,那么一旦我选择要使用的裁剪应用程序,它就会崩溃。为什么我这样做会崩溃?如果我只使用相机,它的效果很好。

我的代码由三个Java文件组成。

主要

package com.goboapp;

import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;


public class Main extends ActionBarActivity {
    private Uri mImageCaptureUri;

    private static final int PICK_FROM_CAMERA = 1;
    private static final int CROP_FROM_CAMERA = 2;
    private static final int PICK_FROM_FILE = 3;

     ImageView imageView1;
     RoundImage roundedImage;
     Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView1 = (ImageView) findViewById(R.id.imageView1);


        //Camera Stuff

        final String [] items           = new String [] {"Take from camera", "Select from gallery"};                
        ArrayAdapter<String> adapter    = new ArrayAdapter<String> (this, android.R.layout.select_dialog_item,items);
        AlertDialog.Builder builder     = new AlertDialog.Builder(this);

        builder.setTitle("Select Image");
        builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
            public void onClick( DialogInterface dialog, int item ) { //pick from camera
                if (item == 0) {
                    Intent intent    = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                    mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
                                       "tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));

                    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

                    try {
                        intent.putExtra("return-data", true);

                        startActivityForResult(intent, PICK_FROM_CAMERA);
                    } catch (ActivityNotFoundException e) {
                        e.printStackTrace();
                    }
                } else { //pick from file
                    Intent intent = new Intent();

                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);

                    startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
                }
            }
        } );

        final AlertDialog dialog = builder.create();

        Button button   = (Button) findViewById(R.id.btn_crop);

        button.setOnClickListener(new View.OnClickListener() {  
            @Override
            public void onClick(View v) {
                dialog.show();
            }
        });

        //END CAMERA STUFF

         }// End OnCreate





    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }//end onOptionsItemSelected

    //CAMERA STUFF

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK) return;

        switch (requestCode) {
            case PICK_FROM_CAMERA:
                doCrop();

                break;

            case PICK_FROM_FILE: 
                mImageCaptureUri = data.getData();

                doCrop();

                break;          

            case CROP_FROM_CAMERA:          
                Bundle extras = data.getExtras();

                if (extras != null) {               
                    Bitmap photo = extras.getParcelable("data");

                   imageView1.setImageBitmap(photo);
                }

                File f = new File(mImageCaptureUri.getPath());            

                if (f.exists()) f.delete();

                break;

        }
    }

      private void doCrop() {
        final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();

        Intent intent = new Intent("com.android.camera.action.CROP");
          intent.setType("image/*");

          List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );

          int size = list.size();

          if (size == 0) {          
            Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();

              return;
          } else {
            intent.setData(mImageCaptureUri);

              intent.putExtra("outputX", 200);
              intent.putExtra("outputY", 200);
              intent.putExtra("aspectX", 1);
              intent.putExtra("aspectY", 1);
              intent.putExtra("scale", true);
              intent.putExtra("return-data", true);

            if (size == 1) {
                Intent i        = new Intent(intent);
                ResolveInfo res = list.get(0);

                i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));

                startActivityForResult(i, CROP_FROM_CAMERA);
            } else {
                for (ResolveInfo res : list) {
                    final CropOption co = new CropOption();

                    co.title    = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo);
                    co.icon     = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo);
                    co.appIntent= new Intent(intent);

                    co.appIntent.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));

                    cropOptions.add(co);
                }

                CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions);

                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("Choose Crop App");
                builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
                    public void onClick( DialogInterface dialog, int item ) {
                        startActivityForResult( cropOptions.get(item).appIntent, CROP_FROM_CAMERA);
                    }
                });

                builder.setOnCancelListener( new DialogInterface.OnCancelListener() {
                    @Override
                    public void onCancel( DialogInterface dialog ) {

                        if (mImageCaptureUri != null ) {
                            getContentResolver().delete(mImageCaptureUri, null, null );
                            mImageCaptureUri = null;
                        }
                    }
                } );

                AlertDialog alert = builder.create();

                alert.show();
            }
          }
    }

      //END CAMERA STUFF




}//end activity

CropOption

package com.goboapp;

import android.content.Intent;
import android.graphics.drawable.Drawable;

public class CropOption {
    public CharSequence title;
    public Drawable icon;
    public Intent appIntent;
}

CropOptionAdapter

package com.goboapp;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import android.content.Context;

import java.util.ArrayList;

public class CropOptionAdapter extends ArrayAdapter<CropOption> {
    private ArrayList<CropOption> mOptions;
    private LayoutInflater mInflater;

    public CropOptionAdapter(Context context, ArrayList<CropOption> options) {
        super(context, R.layout.crop_selector, options);

        mOptions    = options;

        mInflater   = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup group) {
        if (convertView == null)
            convertView = mInflater.inflate(R.layout.crop_selector, null);

        CropOption item = mOptions.get(position);

        if (item != null) {
            ((ImageView) convertView.findViewById(R.id.iv_icon)).setImageDrawable(item.icon);
            ((TextView) convertView.findViewById(R.id.tv_name)).setText(item.title);

            return convertView;
        }

        return null;
    }
}

我使用的XML文件是

activity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.goboapp.Main" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:scaleType="centerCrop"
        android:layout_gravity="center" />

    <Button 
        android:id="@+id/btn_crop"
        android:layout_width="match_parent"          
        android:layout_height="wrap_content"
        android:text="Select image"
        android:layout_marginTop="10dp"
        android:layout_below="@id/imageView1"/>

</RelativeLayout>

crop_selector

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:gravity="center_vertical">

    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""/>
</LinearLayout>

logcat的

11-14 00:33:14.164: E/AndroidRuntime(1755): FATAL EXCEPTION: AsyncTask #1
11-14 00:33:14.164: E/AndroidRuntime(1755): Process: com.motorola.MotGallery2, PID: 1755
11-14 00:33:14.164: E/AndroidRuntime(1755): java.lang.RuntimeException: An error occured while executing doInBackground()
11-14 00:33:14.164: E/AndroidRuntime(1755):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
11-14 00:33:14.164: E/AndroidRuntime(1755):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
11-14 00:33:14.164: E/AndroidRuntime(1755):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
11-14 00:33:14.164: E/AndroidRuntime(1755):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
11-14 00:33:14.164: E/AndroidRuntime(1755):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
11-14 00:33:14.164: E/AndroidRuntime(1755):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-14 00:33:14.164: E/AndroidRuntime(1755):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-14 00:33:14.164: E/AndroidRuntime(1755):     at java.lang.Thread.run(Thread.java:841)
11-14 00:33:14.164: E/AndroidRuntime(1755): Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{44b96898 1755:com.motorola.MotGallery2/u0a40} (pid=1755, uid=10040) requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS
11-14 00:33:14.164: E/AndroidRuntime(1755):     at android.os.Parcel.readException(Parcel.java:1471)
11-14 00:33:14.164: E/AndroidRuntime(1755):     at android.os.Parcel.readException(Parcel.java:1425)
11-14 00:33:14.164: E/AndroidRuntime(1755):     at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:2896)
11-14 00:33:14.164: E/AndroidRuntime(1755):     at android.app.ActivityThread.acquireProvider(ActivityThread.java:4491)
11-14 00:33:14.164: E/AndroidRuntime(1755):     at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2323)
11-14 00:33:14.164: E/AndroidRuntime(1755):     at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1425)
11-14 00:33:14.164: E/AndroidRuntime(1755):     at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1047)
11-14 00:33:14.164: E/AndroidRuntime(1755):     at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:904)
11-14 00:33:14.164: E/AndroidRuntime(1755):     at android.content.ContentResolver.openInputStream(ContentResolver.java:629)
11-14 00:33:14.164: E/AndroidRuntime(1755):     at com.android.gallery3d.filtershow.cache.ImageLoader.loadBitmap(ImageLoader.java:331)
11-14 00:33:14.164: E/AndroidRuntime(1755):     at com.android.gallery3d.filtershow.cache.ImageLoader.loadBitmapBounds(ImageLoader.java:306)
11-14 00:33:14.164: E/AndroidRuntime(1755):     at com.android.gallery3d.filtershow.cache.ImageLoader.loadConstrainedBitmap(ImageLoader.java:359)
11-14 00:33:14.164: E/AndroidRuntime(1755):     at com.android.gallery3d.filtershow.crop.CropActivity$LoadBitmapTask.doInBackground(CropActivity.java:277)
11-14 00:33:14.164: E/AndroidRuntime(1755):     at com.android.gallery3d.filtershow.crop.CropActivity$LoadBitmapTask.doInBackground(CropActivity.java:261)
11-14 00:33:14.164: E/AndroidRuntime(1755):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
11-14 00:33:14.164: E/AndroidRuntime(1755):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-14 00:33:14.164: E/AndroidRuntime(1755):     ... 4 more

这就是我的整个应用程序。

当我选择使用相机拍照,选择裁剪应用程序然后保存照片时,该应用程序可以正常工作。

当我选择从图库中获取照片时,应用程序无效。它打开我的画廊,然后我选择一个图像,然后选择我想用来裁剪图像的应用程序,然后应用程序崩溃。

2 个答案:

答案 0 :(得分:2)

使用android.permission.MANAGE_DOCUMENTS时Kitkat存在问题。您需要做这项工作。

试试这段代码:

  public static final String KITKAT_VALUE = 1002;

  Intent intent;

  if (Build.VERSION.SDK_INT < 19){
       intent = new Intent();
       intent.setAction(Intent.ACTION_GET_CONTENT);
       intent.setType("*/*");
       startActivityForResult(intent, KITKAT_VALUE);
  } 
  else 
  {
       intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
       intent.addCategory(Intent.CATEGORY_OPENABLE);
       intent.setType("*/*");
       startActivityForResult(intent, KITKAT_VALUE);
  }

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
     if (requestCode == KITKAT_VALUE ) {
         if (resultCode == Activity.RESULT_OK) {
         // do your stuff
         }
     }
}

如果这也不起作用,Other work around
参考:Android Gallery on KitKat returns different Uri for Intent.ACTION_GET_CONTENT

希望它会对你有所帮助:)。

答案 1 :(得分:1)

Logcat说:

11-14 00:33:14.164: E/AndroidRuntime(1755): Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{44b96898 1755:com.motorola.MotGallery2/u0a40} (pid=1755, uid=10040) requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS

您需要在应用标记下的清单中添加此权限:

<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />

这将消除崩溃。