简体   繁体   English

无法从文件获取位图

[英]Cannot get Bitmap from File

I cannot retrieve bitmap from the file to which i have written the bitmap. 我无法从已将位图写入其中的文件中检索位图。 There is no error showing.But image is not displaying correctly. 没有显示错误,但是图像显示不正确。 I converted bitmap to bytes and then stored in the internal cache 我将位图转换为字节,然后存储在内部缓存中

Getting image from camera 从相机获取图像

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

Writting image to file 将图像写入文件

                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();

Reading Bitmap 读取位图

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

Is this code correct? 此代码正确吗?

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

try to convert bitmap like this 尝试像这样转换位图

You are createing image file twice and may be as per my opinion, your code will be look like below :- 您正在创建图像文件两次,并且据我的看法,您的代码将如下所示 :-

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

-Pardon me for my poor explanation but if it is still not working you can share or ask again for the help -请原谅我的解释不佳,但如果仍然无法解决,您可以分享或再次寻求帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM