【问题标题】:Android: Can't load images previously taken with the cameraAndroid:无法加载以前用相机拍摄的图像
【发布时间】:2015-02-26 04:15:07
【问题描述】:

我可以在图库中加载手机随附的库存图片。 但是当我尝试从相机目录(在同一个画廊内)加载图像时,它没有显示出来。

但真正奇怪的是,它占用了空间,就好像图像加载成功一样。所以它占用空间,但它是不可见的。 (通过为 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/

【问题讨论】:

    标签: java android image


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2016-04-18
      • 2011-07-15
      • 1970-01-01
      • 2011-10-10
      • 1970-01-01
      • 2016-02-07
      • 1970-01-01
      • 1970-01-01
      • 2017-06-02
      相关资源
      最近更新 更多