librelist archives

« back to archive

problems reading file from SD card

problems reading file from SD card

From:
Marc Meszaros
Date:
2010-10-19 @ 17:50
Hello,

A little context: I have an app that stores some images (book cover
thumbnails) on the SD card and then loads them up for an ImageView in a
custom adapter for a ListActivity. The images (.jpg) are downloaded and
saved properly on the SD card. Using the same code, they display on my
physical phone (Nexus One - 2.2.1) but not in my Android 1.5 emulator.
Activity permissions are correct, and everything is API level 3 compatible.

DBManager.getThumbnail(<String>) returns a File object corresponding to the
file matching the ISBN number.

Code in custom CursorAdapter :

@Override
public void bindView(View view, Context context, Cursor cursor) {
 ImageView cover = (ImageView) view.findViewById(R.id.book_cover);
TextView title = (TextView) view.findViewById(R.id.book_title);
 TextView author = (TextView) view.findViewById(R.id.book_author);

title.setText(cursor.getString(cursor.getColumnIndex(DBHelper.BOOK_FIELD_TITLE)));


author.setText(cursor.getString(cursor.getColumnIndex(DBHelper.BOOK_FIELD_AUTHOR)));
 // try and get both isbn numbers from the result
String isbn10 =
cursor.getString(cursor.getColumnIndex(DBHelper.BOOK_FIELD_ISBN10));
 String isbn13 =
cursor.getString(cursor.getColumnIndex(DBHelper.BOOK_FIELD_ISBN13));
 // check if we got an isbn10 number from query and file exists
if(isbn10 != null && DBManager.getThumbnail(isbn10).exists()){
 cover.setImageURI(Uri.fromFile(DBManager.getThumbnail(isbn10)));
 }
 // check if we got an isbn13 number from query and file exists
else if(isbn13 != null && DBManager.getThumbnail(isbn13).exists()){
 cover.setImageURI(Uri.fromFile(DBManager.getThumbnail(isbn13)));
 }
}


There is also an error when running logcat on the emulator and calling up
the Activity with the ListView:

        I/System.out(  760): resolveUri failed on bad bitmap uri:
file:///sdcard/Papyrus/9780750689533.jpg

The file does exist and can be viewed without any problem using a file
explorer app. I also tried using these alternative ways of setting the
ImageView image based on Google searches for the "resolveUri" problem:


 
cover.setImageBitmap(BitmapFactory.decodeFile(DBManager.getThumbnail(isbn13).getAbsolutePath()));
        AND

 cover.setImageURI(Uri.parse(DBManager.getThumbnail(isbn13).getAbsolutePath()));

Still no joy with these alternatives. Anyone have any ideas why it would
work on Android 2.2 but not Android 1.5?

-- 
Marc Meszaros

Re: [ottawaandroid] problems reading file from SD card

From:
Christopher Saunders
Date:
2010-10-19 @ 17:56
  Are you trying to hit a hard coded location such as '/mnt/sdcard/<etc 
etc>'?

If my memory serves me right, there was a change to where the SDCard is 
located on the newer android platforms.  That's alright though, because 
you can actually ask the system for the SD card location 
programmatically which works back to 1.5 (at least).

This is how you would do it:
     public static boolean hasSDCard() {
         return 

android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
     }

     public static File determineStorageLocation(Context context, String 
storageLocation) {
         File rootDir = null;
         if(IOUtils.hasSDCard()) {
             rootDir = new 
File(android.os.Environment.getExternalStorageDirectory(), storageLocation);
         } else {
             rootDir = new File(context.getFilesDir(), storageLocation);
         }
         return rootDir;
     }



On 10-10-19 1:50 PM, Marc Meszaros wrote:
> Hello,
>
> A little context: I have an app that stores some images (book cover 
> thumbnails) on the SD card and then loads them up for an ImageView in 
> a custom adapter for a ListActivity. The images (.jpg) are downloaded 
> and saved properly on the SD card. Using the same code, they display 
> on my physical phone (Nexus One - 2.2.1) but not in my Android 1.5 
> emulator. Activity permissions are correct, and everything is API 
> level 3 compatible.
>
> DBManager.getThumbnail(<String>) returns a File 
> object corresponding to the file matching the ISBN number.
>
> Code in custom CursorAdapter :
>
> @Override
> public void bindView(View view, Context context, Cursor cursor) {
> ImageView cover = (ImageView) view.findViewById(R.id.book_cover);
> TextView title = (TextView) view.findViewById(R.id.book_title);
> TextView author = (TextView) view.findViewById(R.id.book_author);
> 
title.setText(cursor.getString(cursor.getColumnIndex(DBHelper.BOOK_FIELD_TITLE)));
> 
author.setText(cursor.getString(cursor.getColumnIndex(DBHelper.BOOK_FIELD_AUTHOR)));
> // try and get both isbn numbers from the result
> String isbn10 = 
> cursor.getString(cursor.getColumnIndex(DBHelper.BOOK_FIELD_ISBN10));
> String isbn13 = 
> cursor.getString(cursor.getColumnIndex(DBHelper.BOOK_FIELD_ISBN13));
> // check if we got an isbn10 number from query and file exists
> if(isbn10 != null && DBManager.getThumbnail(isbn10).exists()){
> cover.setImageURI(Uri.fromFile(DBManager.getThumbnail(isbn10)));
> }
> // check if we got an isbn13 number from query and file exists
> else if(isbn13 != null && DBManager.getThumbnail(isbn13).exists()){
> cover.setImageURI(Uri.fromFile(DBManager.getThumbnail(isbn13)));
> }
> }
>
>
> There is also an error when running logcat on the emulator and calling 
> up the Activity with the ListView:
>
>         I/System.out(  760): resolveUri failed on bad bitmap uri: 
> file:///sdcard/Papyrus/9780750689533.jpg
>
> The file does exist and can be viewed without any problem using a file 
> explorer app. I also tried using these alternative ways of setting the 
> ImageView image based on Google searches for the "resolveUri" problem:
>
>    
>  
cover.setImageBitmap(BitmapFactory.decodeFile(DBManager.getThumbnail(isbn13).getAbsolutePath()));
>         AND
>    
>  cover.setImageURI(Uri.parse(DBManager.getThumbnail(isbn13).getAbsolutePath()));
>
> Still no joy with these alternatives. Anyone have any ideas why it 
> would work on Android 2.2 but not Android 1.5?
>
> -- 
> Marc Meszaros


-- 
CS

Re: [ottawaandroid] problems reading file from SD card

From:
Marc Meszaros
Date:
2010-10-20 @ 03:11
It is fixed. Here is how:

I changed the ImageView to be set using:


 cover.setImageURI(Uri.parse(DBManager.getThumbnail(isbn13).getAbsolutePath()));

It still didn't work after that. After a few hours of working on this, it
seemed like it wasn't a problem with reading the file from the SD card. It
was a problem with the file not being properly saved on the SD card in the
first place (the "resolveUri" error from logcat was misleading). For some
reason BitmapFactory.decodeStream() has some funky stuff going on in
previous versions of Android if you don't pass it the exact input stream
class it wants and you don't have a perfect network (ie. never in the real
world).

I followed this solution:

http://stackoverflow.com/questions/1630258/android-problem-bug-with-threadsafeclientconnmanager-downloading-images

More on this problem: http://code.google.com/p/android/issues/detail?id=6066

My code would fail on a physical Android 2.1 device, but not a 2.1 emulator.
It would succeed on a physical Android 2.2 device. Someone made a comment
about the same symptoms I was having in the google bug tracker:

"Interesting (@giles, @pentiumao) I was not able to trigger the bug in a 2.1
android vm. I could only do it on my phone. Even when I put the vm speed
down(and reception) as far as I could... "

So to sum up: on previous versions of Android, BitmapFactory.decodeStream
will fail if you can't read the entire stream at once. Wrapping your stream
in a BufferedInputStream won't work and buffering it yourself using byte
arrays won't work either (I tried both of these). You need a workaround
similar to the ones in the links above.

Hope this helps anyone with similar problems.

On Tue, Oct 19, 2010 at 1:56 PM, Christopher Saunders <
c.saunders322@gmail.com> wrote:

>  Are you trying to hit a hard coded location such as '/mnt/sdcard/<etc
> etc>'?
>
> If my memory serves me right, there was a change to where the SDCard is
> located on the newer android platforms.  That's alright though, because you
> can actually ask the system for the SD card location programmatically which
> works back to 1.5 (at least).
>
> This is how you would do it:
>     public static boolean hasSDCard() {
>         return
> 
android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
>     }
>
>     public static File determineStorageLocation(Context context, String
> storageLocation) {
>         File rootDir = null;
>         if(IOUtils.hasSDCard()) {
>             rootDir = new
> File(android.os.Environment.getExternalStorageDirectory(), storageLocation);
>         } else {
>             rootDir = new File(context.getFilesDir(), storageLocation);
>         }
>         return rootDir;
>     }
>
>
>
> On 10-10-19 1:50 PM, Marc Meszaros wrote:
>
> Hello,
>
>  A little context: I have an app that stores some images (book cover
> thumbnails) on the SD card and then loads them up for an ImageView in a
> custom adapter for a ListActivity. The images (.jpg) are downloaded and
> saved properly on the SD card. Using the same code, they display on my
> physical phone (Nexus One - 2.2.1) but not in my Android 1.5 emulator.
> Activity permissions are correct, and everything is API level 3 compatible.
>
>  DBManager.getThumbnail(<String>) returns a File object corresponding to
> the file matching the ISBN number.
>
>  Code in custom CursorAdapter :
>
>  @Override
>  public void bindView(View view, Context context, Cursor cursor) {
>  ImageView cover = (ImageView) view.findViewById(R.id.book_cover);
>  TextView title = (TextView) view.findViewById(R.id.book_title);
>  TextView author = (TextView) view.findViewById(R.id.book_author);
>
> 
title.setText(cursor.getString(cursor.getColumnIndex(DBHelper.BOOK_FIELD_TITLE)));
>
> 
author.setText(cursor.getString(cursor.getColumnIndex(DBHelper.BOOK_FIELD_AUTHOR)));
>   // try and get both isbn numbers from the result
>  String isbn10 =
> cursor.getString(cursor.getColumnIndex(DBHelper.BOOK_FIELD_ISBN10));
>  String isbn13 =
> cursor.getString(cursor.getColumnIndex(DBHelper.BOOK_FIELD_ISBN13));
>   // check if we got an isbn10 number from query and file exists
>  if(isbn10 != null && DBManager.getThumbnail(isbn10).exists()){
>  cover.setImageURI(Uri.fromFile(DBManager.getThumbnail(isbn10)));
>  }
>  // check if we got an isbn13 number from query and file exists
>  else if(isbn13 != null && DBManager.getThumbnail(isbn13).exists()){
>  cover.setImageURI(Uri.fromFile(DBManager.getThumbnail(isbn13)));
>  }
>  }
>
>
>  There is also an error when running logcat on the emulator and calling up
> the Activity with the ListView:
>
>          I/System.out(  760): resolveUri failed on bad bitmap uri:
> file:///sdcard/Papyrus/9780750689533.jpg
>
>  The file does exist and can be viewed without any problem using a file
> explorer app. I also tried using these alternative ways of setting the
> ImageView image based on Google searches for the "resolveUri" problem:
>
>
>  
cover.setImageBitmap(BitmapFactory.decodeFile(DBManager.getThumbnail(isbn13).getAbsolutePath()));
>         AND
>
>  cover.setImageURI(Uri.parse(DBManager.getThumbnail(isbn13).getAbsolutePath()));
>
>  Still no joy with these alternatives. Anyone have any ideas why it would
> work on Android 2.2 but not Android 1.5?
>
> --
> Marc Meszaros
>
>
>
> --
> CS
>
>


-- 
Marc Meszaros

Re: [ottawaandroid] problems reading file from SD card

From:
BradS4
Date:
2010-10-20 @ 11:15
Great piece of research!  This will come in very handy for our current
project. Thanks!!

Brad

On Tue, Oct 19, 2010 at 11:11 PM, Marc Meszaros <marc.meszaros@gmail.com>wrote:

> It is fixed. Here is how:
>
> I changed the ImageView to be set using:
>
>
>  cover.setImageURI(Uri.parse(DBManager.getThumbnail(isbn13).getAbsolutePath()));
>
> It still didn't work after that. After a few hours of working on this, it
> seemed like it wasn't a problem with reading the file from the SD card. It
> was a problem with the file not being properly saved on the SD card in the
> first place (the "resolveUri" error from logcat was misleading). For some
> reason BitmapFactory.decodeStream() has some funky stuff going on in
> previous versions of Android if you don't pass it the exact input stream
> class it wants and you don't have a perfect network (ie. never in the real
> world).
>
> I followed this solution:
> 
http://stackoverflow.com/questions/1630258/android-problem-bug-with-threadsafeclientconnmanager-downloading-images
>
> More on this problem:
> http://code.google.com/p/android/issues/detail?id=6066
>
> My code would fail on a physical Android 2.1 device, but not a 2.1
> emulator. It would succeed on a physical Android 2.2 device. Someone made a
> comment about the same symptoms I was having in the google bug tracker:
>
> "Interesting (@giles, @pentiumao) I was not able to trigger the bug in a
> 2.1 android vm. I could only do it on my phone. Even when I put the vm speed
> down(and reception) as far as I could... "
>
> So to sum up: on previous versions of Android, BitmapFactory.decodeStream
> will fail if you can't read the entire stream at once. Wrapping your stream
> in a BufferedInputStream won't work and buffering it yourself using byte
> arrays won't work either (I tried both of these). You need a workaround
> similar to the ones in the links above.
>
> Hope this helps anyone with similar problems.
>
> On Tue, Oct 19, 2010 at 1:56 PM, Christopher Saunders <
> c.saunders322@gmail.com> wrote:
>
>>  Are you trying to hit a hard coded location such as '/mnt/sdcard/<etc
>> etc>'?
>>
>> If my memory serves me right, there was a change to where the SDCard is
>> located on the newer android platforms.  That's alright though, because you
>> can actually ask the system for the SD card location programmatically which
>> works back to 1.5 (at least).
>>
>> This is how you would do it:
>>     public static boolean hasSDCard() {
>>         return
>> 
android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
>>     }
>>
>>     public static File determineStorageLocation(Context context, String
>> storageLocation) {
>>         File rootDir = null;
>>         if(IOUtils.hasSDCard()) {
>>             rootDir = new
>> File(android.os.Environment.getExternalStorageDirectory(), storageLocation);
>>         } else {
>>             rootDir = new File(context.getFilesDir(), storageLocation);
>>         }
>>         return rootDir;
>>     }
>>
>>
>>
>> On 10-10-19 1:50 PM, Marc Meszaros wrote:
>>
>> Hello,
>>
>>  A little context: I have an app that stores some images (book cover
>> thumbnails) on the SD card and then loads them up for an ImageView in a
>> custom adapter for a ListActivity. The images (.jpg) are downloaded and
>> saved properly on the SD card. Using the same code, they display on my
>> physical phone (Nexus One - 2.2.1) but not in my Android 1.5 emulator.
>> Activity permissions are correct, and everything is API level 3 compatible.
>>
>>  DBManager.getThumbnail(<String>) returns a File object corresponding to
>> the file matching the ISBN number.
>>
>>  Code in custom CursorAdapter :
>>
>>  @Override
>>  public void bindView(View view, Context context, Cursor cursor) {
>>  ImageView cover = (ImageView) view.findViewById(R.id.book_cover);
>>  TextView title = (TextView) view.findViewById(R.id.book_title);
>>  TextView author = (TextView) view.findViewById(R.id.book_author);
>>
>> 
title.setText(cursor.getString(cursor.getColumnIndex(DBHelper.BOOK_FIELD_TITLE)));
>>
>> 
author.setText(cursor.getString(cursor.getColumnIndex(DBHelper.BOOK_FIELD_AUTHOR)));
>>   // try and get both isbn numbers from the result
>>  String isbn10 =
>> cursor.getString(cursor.getColumnIndex(DBHelper.BOOK_FIELD_ISBN10));
>>  String isbn13 =
>> cursor.getString(cursor.getColumnIndex(DBHelper.BOOK_FIELD_ISBN13));
>>   // check if we got an isbn10 number from query and file exists
>>  if(isbn10 != null && DBManager.getThumbnail(isbn10).exists()){
>>  cover.setImageURI(Uri.fromFile(DBManager.getThumbnail(isbn10)));
>>  }
>>  // check if we got an isbn13 number from query and file exists
>>  else if(isbn13 != null && DBManager.getThumbnail(isbn13).exists()){
>>  cover.setImageURI(Uri.fromFile(DBManager.getThumbnail(isbn13)));
>>  }
>>  }
>>
>>
>>  There is also an error when running logcat on the emulator and calling
>> up the Activity with the ListView:
>>
>>          I/System.out(  760): resolveUri failed on bad bitmap uri:
>> file:///sdcard/Papyrus/9780750689533.jpg
>>
>>  The file does exist and can be viewed without any problem using a file
>> explorer app. I also tried using these alternative ways of setting the
>> ImageView image based on Google searches for the "resolveUri" problem:
>>
>>
>>  
cover.setImageBitmap(BitmapFactory.decodeFile(DBManager.getThumbnail(isbn13).getAbsolutePath()));
>>         AND
>>
>>  
cover.setImageURI(Uri.parse(DBManager.getThumbnail(isbn13).getAbsolutePath()));
>>
>>  Still no joy with these alternatives. Anyone have any ideas why it would
>> work on Android 2.2 but not Android 1.5?
>>
>> --
>> Marc Meszaros
>>
>>
>>
>> --
>> CS
>>
>>
>
>
> --
> Marc Meszaros
>



-- 
Brad