相机文件夹图像未显示在imageview中

时间:2016-02-14 11:24:41

标签: android camera imageview

我正在尝试将图像设置为用户从其图库中选择的图像视图。但是,除非我从相机文件夹中选择图像,否则所有图像似乎都会加载到我的图像视图中。它不会将图像放入我的imageview中。请参阅下图http://i.stack.imgur.com/h6QGG.jpg

征集意见

Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                            MediaStore.Images.Media.INTERNAL_CONTENT_URI);
                    // Start the Intent
                    Log.d(TAG,"STARTING ACTIVITY FOR RESULT");
                    startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);

OnActivityResult

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        Log.d(TAG,"IN ON ACTIVIY RESULT");
            super.onActivityResult(requestCode, resultCode, data);
            try {
                Log.d(TAG, "IN INTERESTS ON ACTIVITY RESULT");
                if (requestCode == RESULT_LOAD_IMAGE && resultCode == Activity.RESULT_OK && null != data) {
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};

                    Cursor cursor = getContext().getContentResolver().query(selectedImage,
                            filePathColumn, null, null, null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String picturePath = cursor.getString(columnIndex);
                    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                    File file = new File(picturePath);
                    bmOptions.inJustDecodeBounds = true;
                    Bitmap original = BitmapFactory.decodeFile(picturePath);

                    file.createNewFile();
                    FileOutputStream ostream = new FileOutputStream(file);

                    Bitmap newBm = Bitmap.createScaledBitmap(original, 900, 1280, true);
                    newBm.compress(Bitmap.CompressFormat.JPEG, 90, ostream);

                    ostream.close();
                    BitmapFactory.decodeFile(picturePath, bmOptions);
                    iconpic.setImageBitmap(newBm);
                    Toast.makeText(getContext(), picturePath, Toast.LENGTH_SHORT)
                            .show();
                    cursor.close();
                    SharedPreferences appPrefs = getContext().getSharedPreferences("com.example.gcmclient_preferences", Context.MODE_PRIVATE);
                    SharedPreferences.Editor prefsEditor = appPrefs.edit();
                    prefsEditor.putString("iconPath", picturePath);
                    prefsEditor.commit();
                    // String picturePath contains the path of selected Image
                }
            }
            catch (Exception e){
                e.printStackTrace();

            }
        }

1 个答案:

答案 0 :(得分:1)

希望这会奏效。

   Uri imageUri;
    File file;

    public int CAMERA_INTENT_CALLED = 100;
    public int GALLERY_INTENT_CALLED = 101;
    public int GALLERY_KITKAT_INTENT_CALLED = 102; 



  if (Build.VERSION.SDK_INT < 19) {
                            Intent intent = new Intent();
                            intent.setType("image/jpeg");
                            intent.setAction(Intent.ACTION_GET_CONTENT);

                            startActivityForResult(Intent.createChooser(intent,
                                    "Select image to promote"),
                                    GALLERY_INTENT_CALLED);

                        } else {
                            Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                            intent.addCategory(Intent.CATEGORY_OPENABLE);
                            intent.setType("image/jpeg");
                            startActivityForResult(intent,


    GALLERY_KITKAT_INTENT_CALLED);
                    }

onActivityResult

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

            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == Activity.RESULT_OK) {
                Uri originalUri = null;


               if (requestCode == GALLERY_INTENT_CALLED) {

                    if (data.getData() != null) {
                        originalUri = data.getData();
                        String path = getRealPathFromURI(originalUri);
                        setImageBitmap(path);
                        originalUri = null;

                    }
                } else if (requestCode == GALLERY_KITKAT_INTENT_CALLED) {
                    if (data.getData() != null) {
                        originalUri = data.getData();

                        String path = getPath(this, originalUri);
                        setImageBitmap(path);
                        originalUri = null;
                    }
                } else if (requestCode == CAMERA_INTENT_CALLED) {

                    Uri selectedImage = imageUri;
                    try {
                        if (selectedImage != null) {
                            getContentResolver().notifyChange(selectedImage, null);
                            String path = getRealPathFromURI(selectedImage);
                            Log.e("Imagepath Camera", path);
                            setImageBitmap(path);
                            imageUri = null;
                        }
                    } catch (Exception e) {

                        Log.e("Camera", e.toString());

                    }

                }

    }

API&lt; 19及以上选择了GalleryImage

private String getRealPathFromURI(Uri contentURI) {
            Cursor cursor = getContentResolver().query(contentURI, null, null,
                    null, null);
            if (cursor == null) { // Source is Dropbox or other similar local file
                                    // path
                return contentURI.getPath();
            } else {
                cursor.moveToFirst();
                int idx = cursor
                        .getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                return cursor.getString(idx);
            }
        }

选择了KITKAT及以上的GalleryImage

public static 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;
    }

    public static 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 static boolean isGooglePhotosUri(Uri uri) {
        return "com.google.android.apps.photos.content".equals(uri
                .getAuthority());
    }

    public static 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 static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri
                .getAuthority());
    }