[英]Android how to use Environment.getExternalStorageDirectory() corretly
Android how to use Environment.getExternalStorageDirectory() Android如何使用Environment.getExternalStorageDirectory()
How can i use Environment.getExternalStorageDirectory() to read/write a image from the SD card ? 如何使用Environment.getExternalStorageDirectory()从SD卡读取/写入图像? or is there a better way to do it?
还是有更好的方法呢?
I did use the source to download e save the image: 我确实使用源代码下载并保存了图像:
try {
URL url = new URL(urlBaseImagem + icone);
URLConnection conection = url.openConnection();
conection.connect();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = conection.getContentLength();
//
// download the file
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream
//File sdCard = Environment.getExternalStoragePublicDirectory(DOWNLOAD_SERVICE);
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File(sdCard.getAbsolutePath() + "/cardapioweb/rest/");
if(directory.exists() == false){
directory.mkdirs();
}
File outputFile = new File(directory, icone);
OutputStream output = new FileOutputStream(outputFile);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
//publishProgress(""+(int)((total*100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
Log.w("CardapioWeb:" , "Imagem " + outputFile.toString() + " salva no sdcard");
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
I did use the source to load e show the image: 我确实使用源来加载e显示图像:
File arq = new File(this.sdCard.getAbsolutePath() + "/cardapioweb/rest/" + filialDataCollection.get(position).get(KEY_ICONE));
//verifica se a imagem foi baixada com sucesso
if(arq.exists() == true){
//deve exibir a imagem do sdcard
File outputFile1 = new File(this.sdCard.getAbsolutePath() + "/cardapioweb/rest/", filialDataCollection.get(position).get(KEY_ICONE));
Drawable imagem = Drawable.createFromPath(outputFile1.toString());
holder.tvWeatherImage.setImageDrawable(imagem);
}
The above codes works perfectly fine on the emulator (Android Virtual Device) with Eclipse !!!! 上面的代码在带有Eclipse的模拟器(Android虚拟设备)上可以正常工作! But when I generate the apk and install on my Galaxy S4, the app can not read images (no error is generated).
但是,当我生成apk并安装在我的Galaxy S4上时,该应用无法读取图像(不会产生错误)。 Just does not display the images.
只是不显示图像。 What could be wrong?
有什么事吗
Put a log in your code to see what 在您的代码中添加日志以查看
outputFile1.toString();
actually points to, is it what you expect? 实际指向的是您所期望的吗?
Personally, I would have used outputFile1.getAbsolutePath(); 就个人而言,我将使用outputFile1.getAbsolutePath();。 NOT the line below:
不是下面的行:
Drawable imagem = Drawable.createFromPath(outputFile1.toString());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.