我在将应用程序的DCIM文件夹中的imageview设置为jpg时遇到问题
这一行代码使我的应用程序变得非常慢,并且经常完全崩溃应用程序。
picturesDirectoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + "/camera/TapTrack/";
imageview.setImageURI(Uri.parse(picturesDirectoryPath + "/TapTrack_164.jpg/"));
我还尝试了另一种方法,将imageview设置为位图,但这也很慢
picturesDirectoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath() + "/camera/TapTrack/";
imageview.setImageBitmap(BitmapFactory.decodeFile(picturesDirectoryPath + "/TapTrack_164.jpg/"));
我只是想知道你们是否知道一种更有效的方法来将imageview =设置为手机上的路径。谢谢你的帮助!
- logcat错误 -
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
- 第二次logcat错误 -
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
答案 0 :(得分:0)
也许是错误的图片目录路径。 如果您的路径像“/mnt/sdcard/TapTrack_164.jpg”
File f = new File(path); Uri imageUri = Uri.fromFile(f);
请注意“/mnt/sdcard/TapTrack_164.jpg/”错误,在路径末尾删除/:“/ mnt / sdcard / TapTrack_164.jpg”
答案 1 :(得分:0)
试试这个。
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();
}
并且不要忘记将以下权限添加到您的清单。
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
如果它适合你,请标记为。
答案 2 :(得分:-3)
看看下面的android文章
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);
}
有效加载大位图 http://developer.android.com/training/displaying-bitmaps/load-bitmap.html