从相机或图库查看图像

时间:2014-01-14 04:12:07

标签: android android-intent android-camera

民间。

我是否需要执行任何额外步骤才能查看Camera文件夹图像?我的应用程序有一个可以从相机或图库中选择的对话框。两者都工作正常,除非我打开相机文件夹中的任何图像时它崩溃并且它与图库中的任何其他图像完美打开。此外,相机选项工作正常,以便我可以拍摄和查看图像。

请帮帮我!!它试图解决这个问题一周了。

AlertDialog.Builder builder = new AlertDialog.Builder(Splash.this);

        builder.setTitle("Choose Image Source");
        builder.setItems(new CharSequence[] { "Gallery", "Camera" },
                new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                        case 0:

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

                            Intent chooser = Intent.createChooser(intent,
                                    "Choose a Picture");
                            startActivityForResult(chooser,
                                    ACTION_REQUEST_GALLERY);

                            break;

                        case 1:

                            Intent cameraIntent = new Intent(
                                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                            String mImageCaptureUri = null;
                            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                                    mImageCaptureUri);
                            startActivityForResult(cameraIntent,
                                    ACTION_REQUEST_CAMERA);

                            break;

                        }
                    }
                });

        builder.show();

        break;

    }

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {

        switch (requestCode) {
        case ACTION_REQUEST_GALLERY:


            Uri selectedImageUri = data.getData();

            try {
                Uri selectedImage = selectedImageUri;
                //getContentResolver().notifyChange(selectedImage, null);
                ContentResolver cr = getContentResolver();

                Bitmap bitmap;
                bitmap = android.provider.MediaStore.Images.Media
                        .getBitmap(cr, selectedImage);
                cap.setImageBitmap(bitmap);

            } catch (Exception e) {
                Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                        .show();
                Log.e("Camera", e.toString());
            }


            break;

        case ACTION_REQUEST_CAMERA:

            Bitmap photo = (Bitmap) data.getExtras().get("data");

            cap.setImageBitmap(photo);

            // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
            Uri tempUri = getImageUri(getApplicationContext(), photo);

            // CALL THIS METHOD TO GET THE ACTUAL PATH
            File finalFile = new File(getRealPathFromURI(tempUri));

            Toast.makeText(getApplicationContext(),
                    finalFile.getAbsolutePath(), 10000).show();

            break;
        }

    }
}

private String getPath(Uri selectedImageUri) {


    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(selectedImageUri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);

}

private String getRealPathFromURI(Uri tempUri) {

    Cursor cursor = getContentResolver().query(tempUri, null, null, null,
            null);
    cursor.moveToFirst();
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    return cursor.getString(idx);

}

private Uri getImageUri(Context applicationContext, Bitmap photo) {

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    photo.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = Images.Media.insertImage(
            applicationContext.getContentResolver(), photo, "Title", null);
    return Uri.parse(path);
}

0 个答案:

没有答案