已更新
我想将位图图像保存到SD卡/设备库中。我尝试了THIS和THIS。代码正在运行,我正在模拟器上检查它。每当我保存图像时,图库中都没有任何内容。请帮忙
日志报告
07-25 02:57:34.345: E/MainActivity(1249): Error in creating fragment
07-25 02:57:35.476: I/Choreographer(1249): Skipped 95 frames! The application may be doing too much work on its main thread.
07-25 02:57:51.955: D/dalvikvm(1249): GC_FOR_ALLOC freed 1409K, 21% free 5888K/7400K, paused 46ms, total 73ms
07-25 02:57:51.955: I/dalvikvm-heap(1249): Grow heap (frag case) to 6.959MB for 1166416-byte allocation
07-25 02:57:52.085: D/dalvikvm(1249): GC_FOR_ALLOC freed 44K, 6% free 6983K/7400K, paused 53ms, total 53ms
07-25 02:57:52.266: I/Choreographer(1249): Skipped 60 frames! The application may be doing too much work on its main thread.
07-25 02:57:55.556: D/dalvikvm(1249): GC_FOR_ALLOC freed 2621K, 34% free 5702K/8568K, paused 42ms, total 44ms
07-25 02:57:55.606: W/System.err(1249): java.io.FileNotFoundException: /storage/sdcard/MyFolder/storage/sdcard/MyFolder/ pic.jpg: open failed: ENOENT (No such file or directory)
07-25 02:57:55.606: W/System.err(1249): at libcore.io.IoBridge.open(IoBridge.java:409)
07-25 02:57:55.606: W/System.err(1249): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
07-25 02:57:55.616: W/System.err(1249): at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
07-25 02:57:55.616: W/System.err(1249): at com.example.navigationexample.MainActivity.saveImage(MainActivity.java:314)
07-25 02:57:55.626: W/System.err(1249): at com.example.navigationexample.MainActivity.onOptionsItemSelected(MainActivity.java:148)
07-25 02:57:55.626: W/System.err(1249): at android.app.Activity.onMenuItemSelected(Activity.java:2566)
07-25 02:57:55.639: W/System.err(1249): at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:372)
07-25 02:57:55.639: W/System.err(1249): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:986)
07-25 02:57:55.646: W/System.err(1249): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
07-25 02:57:55.646: W/System.err(1249): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
07-25 02:57:55.646: W/System.err(1249): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
07-25 02:57:55.658: W/System.err(1249): at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:547)
07-25 02:57:55.658: W/System.err(1249): at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
07-25 02:57:55.658: W/System.err(1249): at android.view.View.performClick(View.java:4240)
07-25 02:57:55.666: W/System.err(1249): at android.view.View$PerformClick.run(View.java:17721)
07-25 02:57:55.676: W/System.err(1249): at android.os.Handler.handleCallback(Handler.java:730)
07-25 02:57:55.676: W/System.err(1249): at android.os.Handler.dispatchMessage(Handler.java:92)
07-25 02:57:55.676: W/System.err(1249): at android.os.Looper.loop(Looper.java:137)
07-25 02:57:55.686: W/System.err(1249): at android.app.ActivityThread.main(ActivityThread.java:5103)
07-25 02:57:55.695: W/System.err(1249): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 02:57:55.695: W/System.err(1249): at java.lang.reflect.Method.invoke(Method.java:525)
07-25 02:57:55.695: W/System.err(1249): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-25 02:57:55.706: W/System.err(1249): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-25 02:57:55.706: W/System.err(1249): at dalvik.system.NativeStart.main(Native Method)
07-25 02:57:55.716: W/System.err(1249): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
07-25 02:57:55.726: W/System.err(1249): at libcore.io.Posix.open(Native Method)
07-25 02:57:55.726: W/System.err(1249): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
07-25 02:57:55.726: W/System.err(1249): at libcore.io.IoBridge.open(IoBridge.java:393)
07-25 02:57:55.736: W/System.err(1249): ... 23 more
07-25 02:57:56.056: I/Choreographer(1249): Skipped 32 frames! The application may be doing too much work on its main thread.
保存功能 public void saveImage(Bitmap bitmap){
String fname = path + " pic" + ".jpg";
File file = new File (dir, fname);
//if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
image.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
在SD卡中制作文件夹的功能。我在setContentView之后调用了它,并在我想保存它时调用saveImage函数
public void makeFolderInSdCard() {
path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyFolder/";
dir = new File(path);
if(!dir.exists())
dir.mkdirs();
}
答案 0 :(得分:0)
您是否在清单文件中添加了权限?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 1 :(得分:0)
在** setContentView()**之后在SD卡中创建一个文件夹,如下所示:
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyFolder/";
File dir = new File(path);
if(!dir.exists())
dir.mkdirs();
然后按如下方式创建一个新文件:
String s = path + "pic" + ".jpg";
File newfile = new File(s);
然后使用FileOutputStream存储图像。希望这可以帮助。另外,为什么使用file.delete()?
答案 2 :(得分:0)
//而不是
String fname = path + " pic" + ".jpg";
File file = new File (dir, fname);
//使用此
String fname = "pic" + ".jpg";
File file = new File (dir , fname);
答案 3 :(得分:0)
正确检查您的日志文件。它显示&#34; java.io.FileNotFoundException:/ storage / sdcard / MyFolder / storage / sdcard / MyFolder / pic.jpg&#34;。
这里&#34; / storage / sdcard / MyFolder&#34;重复,看起来像无效的路径。 所以有效路径应为&#34; /storage/sdcard/MyFolder/pic.jpg"。
答案 4 :(得分:-1)
您需要在AndroidManifest.xml文件中设置权限和功能。在AndroidManifest.xml中添加以下内容
它应该是这样的:
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17" /> <!-- Accessing camera hardware --> <uses-feature android:name="android.hardware.camera" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
然后你需要一个目录名来存储你捕获的图像
private static final String IMAGE_DIRECTORY_NAME =“您的目录名称”;
然后是一个存储图像的文件网址
私人Uri fileUri;
您还将使用MediaStore.EXTRA_OUTPUT指定必须存储图像的路径 示例代码如下:
intent.putExtra(MediaStore.EXTRA_OUTPUT,fileUri);
click here以便日后参考