1

I was trying to display a picture file using ImageView, although I knew that I could decode the file to bitmap directly, but I have to do some other thing to it, so I could only choose byte[].

The code looks like this:

File file = new File(getRealPathFromURI(Uri.parse(ImgUri)));
byte[] beforeData = new byte[(int) file.length()];
try {
    FileInputStream fis = new FileInputStream(file);
    int detectEnd = 0;
    while (detectEnd != -1){detectEnd = fis.read(beforeData, 0, 1024);}
    Bitmap b_t = BitmapFactory.decodeByteArray(beforeData, 0, beforeData.length);
    editImgView.setImageBitmap(b_t);
} catch (FileNotFoundException e) {e.printStackTrace();}
    catch (IOException e) {e.printStackTrace();}

I tried to test out if I read in the picture correctly, so I decode in to Bitmap then try to display it, but it does not display any picture at all.

Is there anything that I misunderstood with FileInputStream? PS. I use log.i to check and found that beforeData's length is normal, but the data inside only get: [B@40c4b110 , which is not like a picture's data.

Thanks in Advance, Desolve.

Oops, thanks you siliconeagle, I forgot to consider that part...(At the very beginning I did) However, it seems like it's not where the main problem is... Now the loop looks like:

    while (pos < beforeData.length){
            read = fis.read(beforeData, pos, 1);
            pos += read;
        }

It's stupid and dummy I know, but the code in this block should work properly, right? However, I still couldn't see any picture in my ImageView.

Another PS: The file's path is at /mnt/sdcard/DCIM/Camera/1310368442822.jpg, size 1485847 bytes, would size cause any trouble?

1 Answer 1

1

you are only reading data into the first 1024 bytes of the array each loop.

fis.read(beforeData, 0, 1024);

you would have to maintain a position counter (int pos) and use another variable (int read) to detect -1

int read=0;
int pos=0;
while (read!=-1) {
    read= fis.read(beforeData, pos, 1024);
    pos+=read;
}

make sure you close your files (fis.close()) as well...

The best way is :

File tmpImgFile = new File("/path");
BitmapFactory.decodeFile(tmpImgFile.getAbsolutePath());
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but there's still problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.