我有一个问题,在我的Android应用程序上创建一个目录并将文件保存到它.我正在使用这段代码来执行此操作:
String filename = "MyApp/MediaTag/MediaTag-"+objectId+".png";
File file = new File(Environment.getExternalStorageDirectory(), filename);
FileOutputStream fos;
fos = new FileOutputStream(file);
fos.write(mediaTagBuffer);
fos.flush();
fos.close();
Run Code Online (Sandbox Code Playgroud)
但这是一个例外:
java.io.FileNotFoundException:/mnt/sdcard/MyApp/MediaCard/MediaCard-0.png(没有这样的文件或目录)
在那条线上: fos = new FileOutputStream(file);
如果我将文件名设置为:"MyApp/MediaTag-"+objectId+"它正在工作,但如果我尝试创建并将文件保存到另一个目录,则会抛出异常.那么任何想法我做错了什么?
还有一个问题:有没有办法让我的文件在外部存储中保密,这样用户就无法在图库中看到它们,只有当他连接他的设备时Disk Drive?
我想在外部存储sdCard中创建一个文件并写入它.我已经通过互联网搜索并尝试但没有得到结果,我已经在Android Manifest文件中添加了权限,我在模拟器上这样做,我正在尝试下面的代码和获取ERRR","无法创建文件".
btnWriteSDFile = (Button) findViewById(R.id.btnWriteSDFile);
btnWriteSDFile.setOnClickListener(new OnClickListener() {
//private Throwable e;
@Override
public void onClick(View v) {
// write on SD card file data from the text box
try {
File myFile = new File("/sdcard/mysdfile.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(txtData.getText());
myOutWriter.close();
fOut.close();
} catch (Exception e) {
Log.e("ERRR", "Could not create file",e);
}
}// onClick
}); // btnWriteSDFile
Run Code Online (Sandbox Code Playgroud) 为什么我应该通过Picasso库下载图像而不是仅使用此代码:
private Bitmap DownloadImage(String URL)
{
Bitmap bitmap = null;
InputStream in = null;
try
{
in = OpenHttpGETConnection(URL);
bitmap = BitmapFactory.decodeStream(in); in.close();
}
catch (Exception e)
{
Log.d("DownloadImage", e.getLocalizedMessage());
}
return bitmap;
}
Run Code Online (Sandbox Code Playgroud)
另一个问题:
毕加索是在UI中还是通过后台线程下载图像?