0

我遇到了奇怪的问题,我有一个用户单击按钮来选择图像。然后我将编码路径存储在 db 中并稍后检索它,因此我在另一个活动中显示图像但我得到以下异常

09-14 01:39:36.195: W/System.err(2241): java.io.FileNotFoundException: No content provider: /external/images/media/1113
09-14 01:39:36.195: W/System.err(2241):     at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:610)
09-14 01:39:36.195: W/System.err(2241):     at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:542)
09-14 01:39:36.195: W/System.err(2241):     at android.content.ContentResolver.openInputStream(ContentResolver.java:377)

我的按钮 OnClick 并存储在数据库代码中:

                Uri selectedImage = imageReturnedIntent.getData();
                InputStream imageStream;
                try {
                    imageStream = getContentResolver().openInputStream(selectedImage);
                    BitmapFactory.Options options=new BitmapFactory.Options();
                    options.inSampleSize = 8;
                    Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream, null, options );
                    ivPictureChosen.setImageBitmap(yourSelectedImage);
                    storeEncodedImageInDb(selectedImage.getEncodedPath());
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Toast.makeText(this, "Couldn't pick image", Toast.LENGTH_SHORT).show();
                }

我的检索和显示图像代码:

            String imagePath = db.getEncodedImage();
            if(imagePath.length()>0){
                Uri mainImgeUri = Uri.parse(imagePath);
                InputStream imageStream;
                try {
                    imageStream = getContentResolver().openInputStream(mainImgeUri);
                    BitmapFactory.Options options=new BitmapFactory.Options();
                    options.inSampleSize = 8;
                    Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream, null, options );
                    mainImage.setImageBitmap(yourSelectedImage);
                } catch (FileNotFoundException e) {

                    e.printStackTrace();
                }
            }

尽管那是我首先得到的编码路径,但为什么找不到文件?

4

2 回答 2

0

这是编码和解码到base64 string和返回的方式base64 string,请看一下。

public static String encodeTobase64(String Path)
{
    Bitmap immagex=Bitmap.createScaledBitmap(BitmapFactory.decodeFile(Path), 140, 120, false);    
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    immagex.compress(Bitmap.CompressFormat.JPEG, 75, baos);
    byte[] b = baos.toByteArray();
    String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);

    Log.e("LOOK", imageEncoded);
    return imageEncoded;
}
public static Bitmap decodeBase64(String input) 
{
    byte[] decodedByte = Base64.decode(input, 0);
    return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length); 
}
于 2013-04-22T06:41:51.497 回答
0

您不仅需要路径,还需要方案等。Uri 看起来像这样: http: //www.stackoverflow.com或 file://foo/bar/image.jpg。

encodedPath 只是其中的路径部分,例如 stackoverflow.com 或 /foo/bar/image.jpg。

尝试将结果存储yourUri.toString()在数据库中并检索Uri.parse(theStringFromDatabase)

于 2012-09-14T06:02:26.533 回答