简体   繁体   中英

Having trouble loading image into imageview

I am having problems setting an imageview to a jpg in the DCIM folder of my app

This one line of code is making my app go really slow, and often crashes the app entirely.

picturesDirectoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + "/camera/TapTrack/";
imageview.setImageURI(Uri.parse(picturesDirectoryPath + "/TapTrack_164.jpg/"));

I also attempted a different way, by setting the imageview to a bitmap, however this is just as slow

picturesDirectoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + "/camera/TapTrack/";
imageview.setImageBitmap(BitmapFactory.decodeFile(picturesDirectoryPath + "/TapTrack_164.jpg/"));

I was just wondering if you guys knew of a more efficient way of setting an imageview = to a path on the phone. Thanks for any help!

--logcat error--

11-16 21:53:50.062: E/AndroidRuntime(1820): FATAL EXCEPTION: main
11-16 21:53:50.062: E/AndroidRuntime(1820): Process: com.example.homeworkreminder, PID: 1820
11-16 21:53:50.062: E/AndroidRuntime(1820): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.homeworkreminder/com.winbusiness.taptrack.EditAssignment}: java.lang.NullPointerException
11-16 21:53:50.062: E/AndroidRuntime(1820):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at android.os.Looper.loop(Looper.java:136)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at android.app.ActivityThread.main(ActivityThread.java:5017)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at java.lang.reflect.Method.invokeNative(Native Method)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at java.lang.reflect.Method.invoke(Method.java:515)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at dalvik.system.NativeStart.main(Native Method)
11-16 21:53:50.062: E/AndroidRuntime(1820): Caused by: java.lang.NullPointerException
11-16 21:53:50.062: E/AndroidRuntime(1820):     at com.winbusiness.taptrack.EditAssignment.checkForPictures(EditAssignment.java:140)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at com.winbusiness.taptrack.EditAssignment.setCurrentValues(EditAssignment.java:135)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at com.winbusiness.taptrack.EditAssignment.declareVariables(EditAssignment.java:112)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at com.winbusiness.taptrack.EditAssignment.onCreate(EditAssignment.java:59)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at android.app.Activity.performCreate(Activity.java:5231)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
11-16 21:53:50.062: E/AndroidRuntime(1820):     ... 11 more

--second logcat error--

11-16 21:53:44.092: E/memtrack(1809): Couldn't load memtrack module (No such file or directory)
11-16 21:53:44.092: E/android.os.Debug(1809): failed to load memtrack module: -2
11-16 21:53:44.922: E/gralloc_goldfish(51): gralloc_alloc: Mismatched usage flags: 246 x 410, usage 333
11-16 21:53:44.922: E/(51): GraphicBufferAlloc::createGraphicBuffer(w=246, h=410) failed (Invalid argument), handle=0x0
11-16 21:53:44.932: E/BufferQueue(382): [ScreenshotClient] dequeueBuffer: SurfaceComposer::createGraphicBuffer failed
11-16 21:53:49.452: E/SoundPool(382): error loading /system/media/audio/ui/Effect_Tick.ogg
11-16 21:53:49.462: E/SoundPool(382): error loading /system/media/audio/ui/Effect_Tick.ogg
11-16 21:53:49.462: E/SoundPool(382): error loading /system/media/audio/ui/Effect_Tick.ogg
11-16 21:53:49.472: E/SoundPool(382): error loading /system/media/audio/ui/Effect_Tick.ogg
11-16 21:53:49.472: E/SoundPool(382): error loading /system/media/audio/ui/Effect_Tick.ogg
11-16 21:53:49.472: E/SoundPool(382): error loading /system/media/audio/ui/KeypressStandard.ogg
11-16 21:53:49.472: E/SoundPool(382): error loading /system/media/audio/ui/KeypressSpacebar.ogg
11-16 21:53:49.472: E/SoundPool(382): error loading /system/media/audio/ui/KeypressDelete.ogg
11-16 21:53:49.482: E/SoundPool(382): error loading /system/media/audio/ui/KeypressReturn.ogg
11-16 21:53:49.482: E/SoundPool(382): error loading /system/media/audio/ui/KeypressInvalid.ogg
11-16 21:53:50.062: E/AndroidRuntime(1820): FATAL EXCEPTION: main
11-16 21:53:50.062: E/AndroidRuntime(1820): Process: com.example.homeworkreminder, PID: 1820
11-16 21:53:50.062: E/AndroidRuntime(1820): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.homeworkreminder/com.winbusiness.taptrack.EditAssignment}: java.lang.NullPointerException
11-16 21:53:50.062: E/AndroidRuntime(1820):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at android.os.Looper.loop(Looper.java:136)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at android.app.ActivityThread.main(ActivityThread.java:5017)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at java.lang.reflect.Method.invokeNative(Native Method)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at java.lang.reflect.Method.invoke(Method.java:515)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at dalvik.system.NativeStart.main(Native Method)
11-16 21:53:50.062: E/AndroidRuntime(1820): Caused by: java.lang.NullPointerException
11-16 21:53:50.062: E/AndroidRuntime(1820):     at com.winbusiness.taptrack.EditAssignment.checkForPictures(EditAssignment.java:140)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at com.winbusiness.taptrack.EditAssignment.setCurrentValues(EditAssignment.java:135)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at com.winbusiness.taptrack.EditAssignment.declareVariables(EditAssignment.java:112)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at com.winbusiness.taptrack.EditAssignment.onCreate(EditAssignment.java:59)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at android.app.Activity.performCreate(Activity.java:5231)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-16 21:53:50.062: E/AndroidRuntime(1820):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
11-16 21:53:50.062: E/AndroidRuntime(1820):     ... 11 more
11-16 22:45:01.352: E/PerformBackupTask(382): Error invoking for backup on @pm@
11-16 22:45:01.382: E/PerformBackupTask(382): Duplicate finish
11-16 22:53:46.892: E/memtrack(1848): Couldn't load memtrack module (No such file or directory)
11-16 22:53:46.912: E/android.os.Debug(1848): failed to load memtrack module: -2
11-16 22:53:58.192: E/memtrack(1863): Couldn't load memtrack module (No such file or directory)
11-16 22:53:58.192: E/android.os.Debug(1863): failed to load memtrack module: -2
11-16 22:53:59.182: E/gralloc_goldfish(51): gralloc_alloc: Mismatched usage flags: 246 x 410, usage 333
11-16 22:53:59.192: E/(51): GraphicBufferAlloc::createGraphicBuffer(w=246, h=410) failed (Invalid argument), handle=0x0
11-16 22:53:59.192: E/BufferQueue(382): [ScreenshotClient] dequeueBuffer: SurfaceComposer::createGraphicBuffer failed
11-16 22:55:41.962: E/memtrack(1892): Couldn't load memtrack module (No such file or directory)
11-16 22:55:41.962: E/android.os.Debug(1892): failed to load memtrack module: -2
11-16 22:55:56.252: E/memtrack(1908): Couldn't load memtrack module (No such file or directory)
11-16 22:55:56.252: E/android.os.Debug(1908): failed to load memtrack module: -2
11-16 22:55:56.942: E/gralloc_goldfish(51): gralloc_alloc: Mismatched usage flags: 246 x 410, usage 333
11-16 22:55:56.952: E/(51): GraphicBufferAlloc::createGraphicBuffer(w=246, h=410) failed (Invalid argument), handle=0x0
11-16 22:55:56.952: E/BufferQueue(382): [ScreenshotClient] dequeueBuffer: SurfaceComposer::createGraphicBuffer failed
11-16 22:57:18.952: E/memtrack(1935): Couldn't load memtrack module (No such file or directory)
11-16 22:57:18.952: E/android.os.Debug(1935): failed to load memtrack module: -2
11-16 22:57:20.032: E/InputDispatcher(382): channel 'b3cd1690 com.example.homeworkreminder/com.winbusiness.taptrack.Home (server)' ~ Channel is unrecoverably broken and will be disposed!
11-16 22:57:33.172: E/memtrack(1952): Couldn't load memtrack module (No such file or directory)
11-16 22:57:33.172: E/android.os.Debug(1952): failed to load memtrack module: -2
11-16 22:57:33.732: E/gralloc_goldfish(51): gralloc_alloc: Mismatched usage flags: 246 x 410, usage 333
11-16 22:57:33.732: E/(51): GraphicBufferAlloc::createGraphicBuffer(w=246, h=410) failed (Invalid argument), handle=0x0
11-16 22:57:33.732: E/BufferQueue(382): [ScreenshotClient] dequeueBuffer: SurfaceComposer::createGraphicBuffer failed
11-16 23:19:14.712: E/PerformBackupTask(382): Error invoking for backup on @pm@
11-16 23:19:14.732: E/PerformBackupTask(382): Duplicate finish

maybe wrong picturesDirectoryPath. if your path like "/mnt/sdcard/TapTrack_164.jpg"

File f = new File(path);  Uri imageUri = Uri.fromFile(f);  

note that "/mnt/sdcard/TapTrack_164.jpg/" was wrong , remove / at end of path :"/mnt/sdcard/TapTrack_164.jpg"

Try this.

File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/your folder path");
File file;
        try {
            file = new File(dir, "yourImage.png");
            imageView.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()));
        } catch (Exception e) {
            e.printStackTrace();
        }

And don't forget to add the following permissions to your manifest.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Mark as up if it works for you.

Have a look at below android article

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
    int reqWidth, int reqHeight) {

// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}

Loading Large Bitmaps Efficiently http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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