ilovebali
ilovebali

Reputation: 543

How to get image from android asset

EDIT

Hi I just newbie on Java and Android development, and I need help to get image from asset folder, I already try many ways to get work but not luck yet.

Here is my folder detail structure

enter image description here

I used Baker Android framework to create my application. Now, what I want is I want call image 1.jpg from image-bali folder and convet it to bitmap, I used this code to decode the file

Bitmap bitmap = BitmapFactory.decodeFile(assetManager + "/books/Individual Villas/images-bali/1.jpg", bitmapOptions);

Here my full code I used for

  AssetManager assetManager = getResources().getAssets();
  String path = "assetManager + "/books/Individual Villas/images-bali/1.jpg";


  BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
  bitmapOptions.inDither = true;// optional
  bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
  Bitmap bitmap = BitmapFactory.decodeFile(path , bitmapOptions);

  Log.d("Test","Bitmap data " + bitmap);

When I'm load the code, it always crash my app. Can anyone please guide me how to do this?

I would appreciate if I can have a detailed procedure, thanks.

Log-Cat :

07-31 16:17:15.694  12542-12542/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.baker.abaker.settings.Configuration.getFilesDirectory(Configuration.java:113)
            at com.baker.abaker.settings.Configuration.getMagazinesDirectory(Configuration.java:130)
            at com.baker.abaker.MagazineActivity$8.onClick(MagazineActivity.java:351)
            at android.view.View.performClick(View.java:4421)
            at android.view.View$PerformClick.run(View.java:18190)
            at android.os.Handler.handleCallback(Handler.java:725)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:175)
            at android.app.ActivityThread.main(ActivityThread.java:5279)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
            at dalvik.system.NativeStart.main(Native Method)
07-31 16:17:15.702      491-496/? E/SurfaceFlinger﹕ #### captureScreenImplLocked
07-31 16:17:16.046      519-571/? E/android.os.Debug﹕ !@Dumpstate > dumpstate -k -t -z -d -o /data/log/dumpstate_app_error
07-31 16:17:24.991      519-749/? E/Watchdog﹕ !@Sync 14914
07-31 16:17:46.343      519-571/? E/ViewRootImpl﹕ sendUserActionEvent() mView == null
07-31 16:17:46.515  12877-12877/? E/com.baker.abaker.GindActivity﹕ Could not load libraries: Couldn't load JavaScriptCore from loader dalvik.system.PathClassLoader[dexPath=/system/framework/android.test.runner.jar:/data/app/com.baker.abaker.test-1.apk,libraryPath=/data/app-lib/com.baker.abaker.test-1]: findLibrary returned null
07-31 16:17:46.694  12877-12877/? E/class com.baker.abaker.GindActivity﹕ USER ACCOUNT COULD NOT BE RETRIEVED, WILL USE ANDROID_ID.
07-31 16:17:46.710  12877-12877/? E/class com.baker.abaker.GindActivity﹕ No valid Google Play Services APK found.
07-31 16:17:47.991      491-496/? E/SurfaceFlinger﹕ #### captureScreenImplLocked
07-31 16:17:48.030     519-1773/? E/EnterpriseContainerManager﹕ ContainerPolicy Service is not yet ready!!!
07-31 16:17:48.163  12877-12877/? E/BitmapFactory﹕ Unable to decode stream: java.io.FileNotFoundException: /file:/android_asset/books/Individual Villas/images-bali/1.jpg: open failed: ENOENT (No such file or directory)
07-31 16:17:48.265  12877-12898/? E/dalvikvm﹕ adjustAdaptiveCoef max=4194304, min=1048576, ut=568
07-31 16:17:48.390  12877-12880/? E/dalvikvm﹕ adjustAdaptiveCoef max=6291456, min=1572864, ut=368
07-31 16:17:48.530      519-882/? E/EnterpriseContainerManager﹕ ContainerPolicy Service is not yet ready!!!
07-31 16:17:50.421  12877-12877/? E/Web Console﹕ Uncaught TypeError: Cannot read property 'top' of null:1073
07-31 16:17:50.718  12877-12877/? E/Web Console﹕ Uncaught TypeError: Cannot read property 'top' of null:1073
07-31 16:17:54.991      519-749/? E/Watchdog﹕ !@Sync 14915

Upvotes: 0

Views: 6697

Answers (5)

user3891073
user3891073

Reputation: 1

Keep your Asset folder reference to Root directory currently it is rooted to your "src/main", for do this copy your "asset folder" and right click on root directory means "ABaker" and paste it. after that delete the asset folder from "src/main" and apply this code.

try {
        // get input stream
        InputStream is = getAssets().open(
                "/books/Individual Villas/images-bali/1.jpg");
        // load image as Drawable
        Drawable d = Drawable.createFromStream(is, null);
        // set image to ImageView
        img_view.setImageDrawable(d);
    } catch (IOException ex) {

    }

Upvotes: 0

Pragnesh Ghoda  シ
Pragnesh Ghoda シ

Reputation: 8337

First Thing you should do is that change name of the folders... use _ (underscore) instead of white-space or - (dash).....

then.... Try This Code to get File...

AssetManager assetManager = getAssets();
    InputStream istr = null;
    try {
        istr = assetManager.open("books/IndividualVillas/images_bali/1.jpg");
    } catch (IOException e) {
        e.printStackTrace();
    }
    Bitmap bitmap = BitmapFactory.decodeStream(istr);
    return bitmap;

Upvotes: 2

Karl-John Chow
Karl-John Chow

Reputation: 805

You should save it as a drawable because you can get a drawable object from an inputstream. use the inputstream to get your asses. Something like this:

try {
        InputStream stream = context.getAssets().open("path/image.jpg");
        Drawable d = Drawable.createFromStream(stream, null);
        yourImageview.setImageDrawable(d);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Source: https://xjaphx.wordpress.com/2011/10/02/store-and-use-files-in-assets/

Upvotes: 0

Meysam
Meysam

Reputation: 694

remove getResources().

    AssetManager assetManager =getAssets();

 Bitmap bitmap = BitmapFactory.decodeFile(assetManager+"/books/IndividualVillas/images-bali/1.jpg");
 Bitmap dest = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);

also remove the space on folder name

Upvotes: 1

Casper Skoubo
Casper Skoubo

Reputation: 310

You can't use white spaces in the path of the assets folder. Unable to decode stream: java.io.FileNotFoundException: /file:/android_asset/books/Individual Villas/images-bali/1.jpg: open failed: ENOENT (No such file or directory)

Upvotes: 0

Related Questions