Android:无法加载以前使用相机拍摄的图像

时间:2015-02-22 22:05:30

标签: java android image

我可以在库中加载手机附带的库存图片。 但是当我尝试从相机目录(位于同一个图库中)加载图像时,它不会显示出来。

但真正奇怪的是,它占用了空间,好像图像已成功加载一样。所以它占用空间,但它是不可见的。 (通过为ImageView提供仅在从相机目录加载图像后才出现的黑色背景来确认这一点。)

这是我添加到AndroidManifest.xml的权限:

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

以下是用于加载图片的代码:

public void loadImagefromGallery(View view) 
{
    // Create intent to Open Image applications like Gallery, Google Photos
    Intent galleryIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    // Start the Intent
    startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}



 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try 
        {
            // When an Image is picked
            if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                    && null != data) 
            {
                // Get the Image from data

                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                // Get the cursor
                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                // Move to first row
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                imgDecodableString = cursor.getString(columnIndex);
                cursor.close();
                ImageView imgView = (ImageView) findViewById(R.id.imgVieww);
                Bitmap bitmapForImage = BitmapFactory.decodeFile(imgDecodableString);

                // Set the Image in ImageView after decoding the String
                imgView.setImageBitmap(bitmapForImage);
            } 
            else 
            {
                Toast.makeText(this, "You haven't picked Image", Toast.LENGTH_LONG).show();
            }
        } 
        catch (Exception e) 
        {
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show();
        }

    }

这是从教程中获得的。 如果您想自己尝试一下,它只有1个类和1个xml文件: http://programmerguru.com/android-tutorial/how-to-upload-image-to-php-server/

1 个答案:

答案 0 :(得分:0)

问题是位图太大了,我不得不缩小它。 这是工作代码:

public void loadImagefromGallery(View view) {
        // Create intent to Open Image applications like Gallery, Google Photos
        Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        // Start the Intent
        startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try 
        {
            // When an Image is picked
            if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                    && null != data) 
            {
                // Get the Image from data

                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };

                // Get the cursor
                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                // Move to first row
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                imgDecodableString = cursor.getString(columnIndex);
                cursor.close();
                ImageView imgView = (ImageView) findViewById(R.id.imgVieww);

                Bitmap bitmapForImage = decodeSampledBitmapFromFile( imgDecodableString, 128, 128 );

                // Set the Image in ImageView after decoding the String
                imgView.setImageBitmap(bitmapForImage);
            } 
            else 
            {
                Toast.makeText(this, "You haven't picked Image", Toast.LENGTH_LONG).show();
            }
        } 
        catch (Exception e) 
        {
            Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show();
        }

    }



    public static Bitmap decodeSampledBitmapFromFile(String imagePath,
            int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(imagePath, options);

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

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

    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) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}