不能创建在SD卡文件 [英] Can't create file in sd-card
问题描述
我试图挽救一个位图文件,然后将其保存在SD卡。 这是我的code:
字符串路径= Environment.getExternalStorageDirectory()的toString()。
档案文件=新的文件(路径,金华盛+JPG);
file.mkdir();
的OutputStream FOUT;
尝试 {
FOUT =新的FileOutputStream(文件);
BitmapMatrix convMatrix =新BitmapMatrix(0);
convMatrix.result.com preSS(Bitmap.Com pressFormat.JPEG,100,FOUT);
fOut.flush();
fOut.close();
}赶上(FileNotFoundException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
另外这款code是一类不延伸Activity.I我只是给这个信息想,也许有一些问题,与此有关。 这是我的LogCat中
05-01 18:00:22.555:W / System.err的(31392):java.io.FileNotFoundException:/mnt/sdcard/Jhs.jpg(权限被拒绝)
05-01 18:00:22.555:W / System.err的(31392):在org.apache.harmony.luni.platform.OSFileSystem.open(本机方法)
05-01 18:00:22.555:W / System.err的(31392):在dalvik.system.BlockGuard $ WrappedFileSystem.open(BlockGuard.java:232)
05-01 18:00:22.555:W / System.err的(31392):在java.io.FileOutputStream中的< INIT>(FileOutputStream.java:94)
05-01 18:00:22.555:W / System.err的(31392):在java.io.FileOutputStream中的< INIT>(FileOutputStream.java:66)
05-01 18:00:22.555:W / System.err的(31392):在com.example.imageprocess.BitmapMatrix.computeConvolution3x3(BitmapMatrix.java:123)
05-01 18:00:22.555:W / System.err的(31392):在com.example.imageprocess.MainActivity.sharpen(MainActivity.java:105)
05-01 18:00:22.555:W / System.err的(31392):在com.example.imageprocess.MainActivity.onCreate(MainActivity.java:51)
05-01 18:00:22.555:W / System.err的(31392):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
05-01 18:00:22.555:W / System.err的(31392):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
05-01 18:00:22.555:W / System.err的(31392):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
05-01 18:00:22.555:W / System.err的(31392):在android.app.ActivityThread.access $ 1500(ActivityThread.java:135)
05-01 18:00:22.555:W / System.err的(31392):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1054)
05-01 18:00:22.555:W / System.err的(31392):在android.os.Handler.dispatchMessage(Handler.java:99)
05-01 18:00:22.565:W / System.err的(31392):在android.os.Looper.loop(Looper.java:150)
05-01 18:00:22.565:W / System.err的(31392):在android.app.ActivityThread.main(ActivityThread.java:4389)
05-01 18:00:22.565:W / System.err的(31392):在java.lang.reflect.Method.invokeNative(本机方法)
05-01 18:00:22.565:W / System.err的(31392):在java.lang.reflect.Method.invoke(Method.java:507)
05-01 18:00:22.565:W / System.err的(31392):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:849)
05-01 18:00:22.565:W / System.err的(31392):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
05-01 18:00:22.565:W / System.err的(31392):在dalvik.system.NativeStart.main(本机方法)
这是的logcat说你没有权限写入文件, 请确保您已声明
<使用-权限的Android:名称=android.permission.WRITE_EXTERNAL_STORAGE/>
在Android清单。
您可以添加这样的:
文件DIR =新的文件(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath()+/%您的文件夹%/);
如果((dir.exists()及!&安培; dir.isDirectory())){
dir.mkdirs();
}
以上您目前的code,如果你想尝试不同的文件夹位置的文件,看看有没有什么帮助。在code将创建目录/ SD卡/下载/您的文件夹/您可以在其中创建文件。请参见这里的存储位置的选择。
然后,您可以做
档案文件=新的文件(目录,金华盛+JPG);
file.createNewFile(); //创建该文件第一!
的OutputStream FOUT;
尝试 {
FOUT =新的FileOutputStream(文件);
....
}
等在您的文章。
I am trying to save a bitmap to a file and then saving it on sd-card. Here's my code:
String path = Environment.getExternalStorageDirectory().toString();
File file = new File(path, "Jhs"+".jpg");
file.mkdir();
OutputStream fOut;
try {
fOut = new FileOutputStream(file);
BitmapMatrix convMatrix = new BitmapMatrix(0);
convMatrix.result.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Also this code is in a class which does not extend Activity.I am just giving this information thinking that maybe there is some issue with this. Here's my LogCat
05-01 18:00:22.555: W/System.err(31392): java.io.FileNotFoundException: /mnt/sdcard/Jhs.jpg (Permission denied)
05-01 18:00:22.555: W/System.err(31392): at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
05-01 18:00:22.555: W/System.err(31392): at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
05-01 18:00:22.555: W/System.err(31392): at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
05-01 18:00:22.555: W/System.err(31392): at java.io.FileOutputStream.<init>(FileOutputStream.java:66)
05-01 18:00:22.555: W/System.err(31392): at com.example.imageprocess.BitmapMatrix.computeConvolution3x3(BitmapMatrix.java:123)
05-01 18:00:22.555: W/System.err(31392): at com.example.imageprocess.MainActivity.sharpen(MainActivity.java:105)
05-01 18:00:22.555: W/System.err(31392): at com.example.imageprocess.MainActivity.onCreate(MainActivity.java:51)
05-01 18:00:22.555: W/System.err(31392): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
05-01 18:00:22.555: W/System.err(31392): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
05-01 18:00:22.555: W/System.err(31392): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
05-01 18:00:22.555: W/System.err(31392): at android.app.ActivityThread.access$1500(ActivityThread.java:135)
05-01 18:00:22.555: W/System.err(31392): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
05-01 18:00:22.555: W/System.err(31392): at android.os.Handler.dispatchMessage(Handler.java:99)
05-01 18:00:22.565: W/System.err(31392): at android.os.Looper.loop(Looper.java:150)
05-01 18:00:22.565: W/System.err(31392): at android.app.ActivityThread.main(ActivityThread.java:4389)
05-01 18:00:22.565: W/System.err(31392): at java.lang.reflect.Method.invokeNative(Native Method)
05-01 18:00:22.565: W/System.err(31392): at java.lang.reflect.Method.invoke(Method.java:507)
05-01 18:00:22.565: W/System.err(31392): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
05-01 18:00:22.565: W/System.err(31392): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
05-01 18:00:22.565: W/System.err(31392): at dalvik.system.NativeStart.main(Native Method)
That logcat is saying you don't have permission to write the file, Make sure you have declared
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in the android manifest.
You can add this:
File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath()+ "/%YOUR_FOLDER%/");
if (!(dir.exists() && dir.isDirectory())) {
dir.mkdirs();
}
above your current code if you want to try a different folder location for the file and see if that helps. The code will create the directory /sdcard/Downloads/YOUR_FOLDER/ in which you can create the file. See here for storage location options.
You can then do
File file = new File(dir, "Jhs"+".jpg");
file.createNewFile(); //CREATE THE FILE FIRST!
OutputStream fOut;
try {
fOut = new FileOutputStream(file);
....
}
etc as in your post.
这篇关于不能创建在SD卡文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!