当前位置: 首页 > 知识库问答 >
问题:

Android 4.4.2-相机uri返回null和裁剪功能不起作用?

司徒兴德
2023-03-14

我正试图单击image from camera并显示到imageview,但onactivityresult URI返回NULL。但是下面的代码在JellyBean、ICS上运行良好,具有裁剪功能。

呼叫意图

                Intent pickIntent = new Intent();
                pickIntent.setType("image/*");
                pickIntent.setAction(Intent.ACTION_GET_CONTENT);
                pickIntent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                // we will handle the returned data in onActivityResult
                String pickTitle = "Select or take a new Picture";
                // startActivityForResult(captureIntent, 1);
                Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]
                { captureIntent });
                startActivityForResult(chooserIntent, GlobalVariables.GALLERY_MODE);
@SuppressLint("NewApi")
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        try
        {
            if (resultCode == RESULT_OK)
            {
                // user is returning from capturing an image using the camera
                if (requestCode == GlobalVariables.GALLERY_MODE)
                {
                    // get the Uri for the captured image
                    picUri = data.getData();
                    System.out.println("picUri      =======     => "+picUri);
                    boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
                    if(isKitKat)
                    {
                        System.out.println(new GraphicsUtil().getPath(RegistrationActivity.this, picUri));
                        String filePath = new GraphicsUtil().getPath(RegistrationActivity.this, picUri);
                        System.out.println("filepath            => "+filePath);
                        bitmapPreview = null;
                        bitmapPreview = BitmapFactory.decodeFile(filePath);
                        System.out.println("bitmapPreview           => "+bitmapPreview );
                        imageViewUser.setImageBitmap(new GraphicsUtil().getCircleBitmap(GraphicsUtil.decodeSampledBitmapFromResource(filePath,
                                120, 120)));
                    }
                    else
                    {
                        performCrop(picUri, PIC_CROP, RegistrationActivity.this);
                    }
                }
                // user is returning from cropping the image
                else if (requestCode == PIC_CROP)
                {
                    String filePath = new File (Environment.getExternalStorageDirectory() + "/temporary_holder.jpg").getAbsolutePath();
                    bitmapPreview = null;
                    bitmapPreview = BitmapFactory.decodeFile(filePath);
                    imageViewUser.setImageBitmap(new GraphicsUtil().getCircleBitmap(GraphicsUtil.decodeSampledBitmapFromResource(filePath,
                            120, 120)));
                    // imageViewUser.setImageBitmap(new
                    // GraphicsUtil().getCircleBitmap(bitmapPreview));
                    File f = new File(Environment.getExternalStorageDirectory() + "/temporary_holder.jpg");
                    if (f.exists())
                    {
                        f.delete();
                    }
                }
            }
        }
        catch (Exception e)
        {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
public class GraphicsUtil
{

    /*
     * Draw image in circular shape Note: change the pixel size if you want
     * image small or large
     */
    public Bitmap getCircleBitmap(Bitmap bitmap)
    {
        Bitmap output;
        Canvas canvas = null;
        final int color = 0xffff0000;
        final Paint paint = new Paint();
        Rect rect = null;
        if (bitmap.getHeight() > 501)
        {
            output = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
            canvas = new Canvas(output);
            rect = new Rect(0, 0, 500, 500);
        }
        else
        {
            //System.out.println("output            else =======");
            bitmap = Bitmap.createScaledBitmap(bitmap, 500, 500, false);
            output = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
            canvas = new Canvas(output);
            rect = new Rect(0, 0, 500, 500);
        }
        final RectF rectF = new RectF(rect);

        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setFilterBitmap(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawOval(rectF, paint);

        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth((float) 1);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }

    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
    {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth)
        {
            // Calculate ratios of height and width to requested height and
            // width
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            // Choose the smallest ratio as inSampleSize value, this will
            // guarantee
            // a final image with both dimensions larger than or equal to the
            // requested height and width.
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }

        return inSampleSize;
    }

    public static Bitmap decodeSampledBitmapFromResource(String path, int reqWidth, int reqHeight)
    {
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(path, options);
    }


    @SuppressLint("NewApi")
    public String getPath(final Context context, final Uri uri) {

        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

        // DocumentProvider
        if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
            // ExternalStorageProvider
            if (isExternalStorageDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                if ("primary".equalsIgnoreCase(type)) {
                    return Environment.getExternalStorageDirectory() + "/" + split[1];
                }

                // TODO handle non-primary volumes
            }
            // DownloadsProvider
            else if (isDownloadsDocument(uri)) {

                final String id = DocumentsContract.getDocumentId(uri);
                final Uri contentUri = ContentUris.withAppendedId(
                        Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

                return getDataColumn(context, contentUri, null, null);
            }
            // MediaProvider
            else if (isMediaDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                Uri contentUri = null;
                if ("image".equals(type)) {
                    contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                } else if ("video".equals(type)) {
                    contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else if ("audio".equals(type)) {
                    contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                }

                final String selection = "_id=?";
                final String[] selectionArgs = new String[] {
                        split[1]
                };

                return getDataColumn(context, contentUri, selection, selectionArgs);
            }
        }
        // MediaStore (and general)
        else if ("content".equalsIgnoreCase(uri.getScheme())) {

            // Return the remote address
            if (isGooglePhotosUri(uri))
                return uri.getLastPathSegment();

            return getDataColumn(context, uri, null, null);
        }
        // File
        else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }

        return null;
    }

    /**
     * Get the value of the data column for this Uri. This is useful for
     * MediaStore Uris, and other file-based ContentProviders.
     *
     * @param context The context.
     * @param uri The Uri to query.
     * @param selection (Optional) Filter used in the query.
     * @param selectionArgs (Optional) Selection arguments used in the query.
     * @return The value of the _data column, which is typically a file path.
     */
    public String getDataColumn(Context context, Uri uri, String selection,
            String[] selectionArgs) {

        Cursor cursor = null;
        final String column = "_data";
        final String[] projection = {
                column
        };

        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                    null);
            if (cursor != null && cursor.moveToFirst()) {
                final int index = cursor.getColumnIndexOrThrow(column);
                return cursor.getString(index);
            }
        } finally {
            if (cursor != null)
                cursor.close();
        }
        return null;
    }


    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is ExternalStorageProvider.
     */
    public boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is DownloadsProvider.
     */
    public boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is MediaProvider.
     */
    public boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is Google Photos.
     */
    public boolean isGooglePhotosUri(Uri uri) {
        return "com.google.android.apps.photos.content".equals(uri.getAuthority());
    }
}
05-08 17:25:55.697: I/System.out(20894): picUri     =======     => null
05-08 17:25:55.698: W/System.err(20894): java.lang.NullPointerException
05-08 17:25:55.711: W/System.err(20894):    at android.provider.DocumentsContract.isDocumentUri(DocumentsContract.java:587)
05-08 17:25:55.712: W/System.err(20894):    at com.asiaelites.utils.GraphicsUtil.getPath(GraphicsUtil.java:119)
05-08 17:25:55.712: W/System.err(20894):    at com.asiaelites.RegistrationActivity.onActivityResult(RegistrationActivity.java:489)
05-08 17:25:55.712: W/System.err(20894):    at android.app.Activity.dispatchActivityResult(Activity.java:5446)
05-08 17:25:55.712: W/System.err(20894):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3442)
05-08 17:25:55.712: W/System.err(20894):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2802)
05-08 17:25:55.712: W/System.err(20894):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2859)
05-08 17:25:55.712: W/System.err(20894):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2274)
05-08 17:25:55.712: W/System.err(20894):    at android.app.ActivityThread.access$800(ActivityThread.java:139)
05-08 17:25:55.712: W/System.err(20894):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
05-08 17:25:55.712: W/System.err(20894):    at android.os.Handler.dispatchMessage(Handler.java:102)
05-08 17:25:55.712: W/System.err(20894):    at android.os.Looper.loop(Looper.java:136)
05-08 17:25:55.712: W/System.err(20894):    at android.app.ActivityThread.main(ActivityThread.java:5102)
05-08 17:25:55.712: W/System.err(20894):    at java.lang.reflect.Method.invokeNative(Native Method)
05-08 17:25:55.713: W/System.err(20894):    at java.lang.reflect.Method.invoke(Method.java:515)
05-08 17:25:55.713: W/System.err(20894):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
05-08 17:25:55.713: W/System.err(20894):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
05-08 17:25:55.713: W/System.err(20894):    at dalvik.system.NativeStart.main(Native Method)

共有1个答案

雷逸仙
2023-03-14

此工作用于kitkat

public class YourActivity  extends Activity{

private static final int SELECT_PICTURE = 1;

private String selectedImagePath;
private ImageView imageView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.browsepicture);
    imageView = (ImageView)findViewById(R.id.imageView1);

    ((Button) findViewById(R.id.button1))
            .setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(intent,
                            "Select Picture"), SELECT_PICTURE);
                }
            });
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            if (Build.VERSION.SDK_INT < 19) {
                selectedImagePath = getPath(selectedImageUri);
                Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath);
                imageView.setImageBitmap(bitmap);

            }
            else {
                ParcelFileDescriptor parcelFileDescriptor;
                try {
                    parcelFileDescriptor = getContentResolver().openFileDescriptor(selectedImageUri, "r");
                    FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
                    Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
                    parcelFileDescriptor.close();
                    imageView.setImageBitmap(image);

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {

                    e.printStackTrace();
                }
            }
        }
    }
}


public String getPath(Uri uri) {
        if( uri == null ) {
            return null;
        }
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if( cursor != null ){
            int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        return uri.getPath();
}

}

For Cropping image 

    private void cropImage() {
        // Use existing crop activity.
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(capturedImageUri, IMAGE_UNSPECIFIED);

        // Specify image size
        intent.putExtra("outputX", IMAGE_DIMENSION);
        intent.putExtra("outputY", IMAGE_DIMENSION);

        // Specify aspect ratio, 1:1
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", true);
        // REQUEST_CODE_CROP_PHOTO is an integer tag you defined to
        // identify the activity in onActivityResult() when it returns
        startActivityForResult(intent, REQ_CODE_CROP_PHOTO);
    }
 类似资料:
  • 本文向大家介绍iOS实现裁剪框和图片剪裁功能,包括了iOS实现裁剪框和图片剪裁功能的使用技巧和注意事项,需要的朋友参考一下 图片处理中经常用的图片剪裁,就是通过剪裁框确定图片剪裁的区域,然后剪去该区域的图片,今天实现了一下,其实图片剪裁本身不难,主要剪裁框封装发了点时间,主要功能可以拖动四个角缩放,但不能超出父视图,拖动四个边单方向缩放,不能超出父视图,拖动中间部分单单移动,不改变大小,不能超出父

  • 问题内容: 我了解到删除前导和尾随空格。但是在我的情况下,它无法正常工作,我正在尝试使用以下代码,但是输出的前导和尾随空格。但是我的期望是文本没有开头和结尾的空格。这是我的代码。 请帮我 问题答案: 您需要将结果重新分配给: 请记住, Java中的字符串是不可变的 ,因此 几乎 所有的String类方法都将创建并返回新的字符串,而不是就地修改字符串。 尽管这是题外话,但是(正如我在那所说的),值得

  • 当我裁剪图像时,应用程序在Android7.0中崩溃了。 异常:02-06 12:40:53.717 567 1-5685/com.tribyte.itutor2 E/databaseutils:将异常写入包java.lang.securityException:拒绝权限:写入android.support.v4.content.fileProvider uri内容: 示例代码:

  • 我正在制作一个程序,每次你点击一个按钮,它会显示你在文本视图中复制了什么文本 但是当我调用(ClipboardManager)getSystemService(CLIPBOARD_SERVICE)时,它返回null; 我已经复制了一些可用的文本(“ABC”),但它不起作用 我不知道剪贴板是否需要任何用户权限?在我的清单中,我没有写任何关于权限的内容 请帮帮我\ 这是我的密码: 这是我的日志:

  • 我正试着拍一张照片并把它保存到一个自定义的位置- onActivityResult()中的数据为空。我错过了什么?

  • 并重写,如下所示: 在Android Pre-Lollipop的设备上,我能够获得并将其显示在中。但是,在Android Lollipop上,我总是从得到。 我在谷歌上搜索了很多,但关于在AndroidLollipop上裁剪图像的有用信息很少。