android - 为什么有些图片不能在ImageView(Android)上显示?

标签 android image imageview gallery

当我从我的图库中选择一张图片时,会显示屏幕截图,但不会显示一些从相机拍摄的图片。

更具体地说,使用系统相机应用程序拍摄的图像无法显示,而使用 Camera360 拍摄的图像可以显示。

我想知道我的代码是否有任何问题。如果没有,可能是我手机的问题?

提前致谢。抱歉,我的英语不是很好。

private static final int SELECT_PICTURE = 1;

private String selectedImagePath;
private ImageView img;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_get_image);

    ((Button) findViewById(R.id.Button01))
            .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) {
            img = (ImageView) findViewById(R.id.ImageView01);
            Uri selectedImageUri = data.getData();

            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);
            Drawable image;
            try {
                InputStream inputStream = getContentResolver()
                        .openInputStream(selectedImageUri);
                image = Drawable.createFromStream(inputStream, "file///"
                        + selectedImagePath.toString());
            } catch (FileNotFoundException e) {
                image = getResources().getDrawable(R.drawable.ic_launcher);
            }
            img.setImageDrawable(null);
            img.setImageDrawable(image);

        }
    }
}

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

最佳答案

哦,我发现问题了。我的图片太大了。更新了代码并进行了以下几项改进:

private static Context context;

private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;

public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;

private static final int SELECT_PICTURE = 3;

private String selectedImagePath;
private static String imageFilePath;
private static String videoFilePath;
private Uri fileUri;

private Bitmap bitmap;

private Button btnGallery, btnCamera;

private Intent selectPictureIntent;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_get_image);

    context = this;

    btnGallery = (Button) findViewById(R.id.Button01);
    btnCamera = (Button) findViewById(R.id.Button02);

    btnGallery.setOnClickListener(this);
    btnCamera.setOnClickListener(this);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            Log.d("bitmap", selectedImageUri.getScheme());
            selectedImagePath = getPath(selectedImageUri);
            if (selectedImagePath != null) {
                // Selected image is local image
                Bitmap b = new BitmapDrawable(context.getResources(),
                        selectedImagePath).getBitmap();
                int i = (int) (b.getHeight() * (512.0 / b.getWidth()));
                bitmap = Bitmap.createScaledBitmap(b, 512, i, true);
            } else {
                // Selected image is Picasa image
                loadPicasaImageFromGallery(selectedImageUri);
            }
            ImageView img = (ImageView) findViewById(R.id.ImageView01);
            img.setImageBitmap(bitmap);
        }
    }

    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Image captured and saved to fileUri specified in the Intent
            addImageToGallery(imageFilePath, context);
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
        }
    }

    if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Video captured and saved to fileUri specified in the Intent
            addVideoToGallery(videoFilePath, context);
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the video capture
        } else {
            // Video capture failed, advise user
        }
    }
}

public Bitmap getBitmapFromUri(Uri uri) throws IOException {
    ParcelFileDescriptor parcelFileDescriptor = getContentResolver()
            .openFileDescriptor(uri, "r");
    FileDescriptor fileDescriptor = parcelFileDescriptor
            .getFileDescriptor();
    Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);
    parcelFileDescriptor.close();
    return image;
}

public String getPath(Uri uri) {
    String[] projection = { MediaColumns.DATA };
    Cursor cursor = getContentResolver().query(uri, projection, null, null,
            null);
    if (cursor != null) {
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
        String filePath = cursor.getString(columnIndex);
        cursor.close();
        return filePath;
    } else
        return uri.getPath();
}

private void loadPicasaImageFromGallery(final Uri uri) {
    String[] projection = { MediaColumns.DATA, MediaColumns.DISPLAY_NAME };
    Cursor cursor = getContentResolver().query(uri, projection, null, null,
            null);
    if (cursor != null) {
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(MediaColumns.DISPLAY_NAME);
        if (columnIndex != -1) {
            new Thread(new Runnable() {
                // NEW THREAD BECAUSE NETWORK REQUEST WILL BE MADE THAT WILL
                // BE A LONG PROCESS & BLOCK UI
                // IF CALLED IN UI THREAD
                public void run() {
                    try {
                        Bitmap bm = android.provider.MediaStore.Images.Media
                                .getBitmap(getContentResolver(), uri);
                        int i = (int) (bm.getHeight() * (512.0 / bm
                                .getWidth()));
                        bitmap = Bitmap
                                .createScaledBitmap(bm, 512, i, true);
                        // THIS IS THE BITMAP IMAGE WE ARE LOOKING FOR
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }).start();

        }
    }
    cursor.close();
}

/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type) {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            "MappyDiary");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d("MappyDiary", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
            .format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        imageFilePath = mediaStorageDir.getPath() + File.separator + "IMG_"
                + timeStamp + ".jpg";
        mediaFile = new File(imageFilePath);
    } else if (type == MEDIA_TYPE_VIDEO) {
        videoFilePath = mediaStorageDir.getPath() + File.separator + "VID_"
                + timeStamp + ".mp4";
        mediaFile = new File(videoFilePath);
    } else {
        return null;
    }

    return mediaFile;
}

public static void addImageToGallery(final String filePath,
        final Context context) {

    ContentValues values = new ContentValues();

    values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
    values.put(Images.Media.MIME_TYPE, "image/jpeg");
    values.put(MediaStore.MediaColumns.DATA, filePath);

    context.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI,
            values);
}

public static void addVideoToGallery(final String filePath,
        final Context context) {

    ContentValues values = new ContentValues();

    values.put(Video.Media.DATE_TAKEN, System.currentTimeMillis());
    values.put(Video.Media.MIME_TYPE, "video/mp4");
    values.put(MediaStore.MediaColumns.DATA, filePath);

    context.getContentResolver().insert(Video.Media.EXTERNAL_CONTENT_URI,
            values);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.Button01:
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        selectPictureIntent = Intent
                .createChooser(intent, "Select Picture");
        startActivityForResult(selectPictureIntent, SELECT_PICTURE);
        break;

    case R.id.Button02:
        // create Intent to take a picture and return control to the calling
        // application
        Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
        intent2.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        // start the image capture Intent
        startActivityForResult(intent2, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    default:
        break;
    }

}

关于android - 为什么有些图片不能在ImageView(Android)上显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24551900/

相关文章:

python - 如何检查 RGB 图像是否只包含一种颜色?

android - 不是 DRM 文件,正常打开

android - 为什么 setLayoutParams 不改变我的 ImageView 的大小?

android-XYZTouristAttractions 谷歌的例子 : error while trying to compile

android - 应用程序开发的新手。创建一个将一个类链接到另一个类的按钮?

javascript - 上传多个文件显示错误代码 : 3

java - 如何知道 ImageView 何时完成加载图像?

android - 如何在 Facebook F8 之后在 android 中获取 Facebook 好友

ios - 通过文件URL远程获取图片文件尺寸

php - 如何使用 PHP 和 GD 解决字体花饰问题