修改
嗨,我只是Java和Android开发的新手,我需要帮助从资产文件夹中获取图像,我已经尝试了很多方法来获得工作,但还没有运气。
这是我的文件夹详细信息结构
我使用Baker Android框架来创建我的应用程序。 现在,我想要的是我想从image-bali文件夹调用图像1.jpg并将其传送到位图,我用这段代码来解码文件
Bitmap bitmap = BitmapFactory.decodeFile(assetManager + "/books/Individual Villas/images-bali/1.jpg", bitmapOptions);
这是我用于
的完整代码 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);
当我加载代码时,它总是会崩溃我的应用。任何人都可以指导我如何做到这一点?
如果我能有详细的程序,我将不胜感激,谢谢。
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
答案 0 :(得分:1)
删除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);
还会删除文件夹名称
上的空格答案 1 :(得分:1)
你应该做的第一件事就是改变文件夹的名称...... 使用_(下划线)代替白色空格或 - (破折号)......
然后.... 尝试使用此代码获取文件...
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;
答案 2 :(得分:0)
您不能在assets文件夹的路径中使用空格。 无法解码流:java.io.FileNotFoundException:/ file:/ android_asset / books / Individual Villas / images-bali / 1.jpg:open failed:ENOENT(没有这样的文件或目录)
答案 3 :(得分:0)
您应该将其保存为drawable,因为您可以从输入流中获取可绘制对象。使用输入流来获取您的评估。像这样:
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();
}
来源:https://xjaphx.wordpress.com/2011/10/02/store-and-use-files-in-assets/
答案 4 :(得分:0)
将您的资源文件夹引用保存到根目录,目前已根据您的" src / main" 执行此操作复制您的"资产文件夹&# 34; 并右键单击根目录意味着" ABaker"并粘贴它。之后从" src / main" 中删除资产文件夹 并应用此代码。
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) {
}