簡體   English   中英

無法從文件獲取位圖

[英]Cannot get Bitmap from File

我無法從已將位圖寫入其中的文件中檢索位圖。 沒有顯示錯誤,但是圖像顯示不正確。 我將位圖轉換為字節,然后存儲在內部緩存中

從相機獲取圖像

Intent cameraIntent = new Intent(
        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(
        Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
        "pic.jpg");
img = Uri.fromFile(photo);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, img);
startActivityForResult(cameraIntent, CAMERA_REQUEST);

將圖像寫入文件

                Bitmap takenImage =BitmapFactory.decodeFile(img.getPath());   
File folder = getCacheDir();
                    File myFile = new File(folder, "myImage.bmp");
                    FileOutputStream fos = new FileOutputStream(myFile);
                    FileInputStream fis = new FileInputStream(img.getPath());
                    byte[] image = new byte[fis.available()];
                    fos.write(image);
                    fos.flush();
                    fos.close();
                    fis.close();

讀取位圖

File folder = getCacheDir();
File myFile = new File(folder, "myImage.bmp");
Bitmap bmp=BitmapFactory.decodeFile(myFile.getAbsolutePath());
imgv.setImageBitmap(bmp);

此代碼正確嗎?

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmap.compress(CompressFormat.JPEG, 100, bos);
            byte[] data = bos.toByteArray();

嘗試像這樣轉換位圖

您正在創建圖像文件兩次,並且據我的看法,您的代碼將如下所示 :-

File myFile = new File( Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
    "pic.jpg");
Bitmap bmp=BitmapFactory.decodeFile(myFile.getAbsolutePath());
imgv.setImageBitmap(bmp);

-請原諒我的解釋不佳,但如果仍然無法解決,您可以分享或再次尋求幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM