我已经通过服务下载了许多 png 文件,现在我正在尝试使用它们。它们进入用户的 SD 卡。我已经确认每个文件都在卡上。但是当我尝试将它们中的任何一个设置为 ImageView 时,我会得到空白。
因此,然后我通过使用手机的图片查看器手动尝试在手机上显示文件来查看文件是否完好无损。不会打开任何文件。我想知道的是,在下载需要使它们以 png 文件(或位图文件)形式查看的文件后,我是否遗漏了任何步骤。这是我用于下载文件的服务中的代码:
public class DownloadPicture extends IntentService {
private int result = Activity.RESULT_CANCELED;
public static final String FILENAME = "filename";
public static final String FILEPATH = "filepath";
public static final String RESULT = "result";
public static final String NOTIFICATION "com.mydomain.myapp.MyBroadcastReceiver";
public DownloadPicture() {
super("DownloadPicture");
}
public DownloadPicture(String name) {
super(name);
}
@Override
protected void onHandleIntent(Intent intent) {
String fileName = intent.getStringExtra(FILENAME);
String urlPath = this.getResources().getString(R.string.imagesURL) + fileName;
System.err.println("starting download of: " + fileName);
System.err.println(urlPath);
File output = new File(Environment.getExternalStorageDirectory(), fileName);
if (output.exists()) {output.delete();}
InputStream stream = null;
FileOutputStream fos = null;
try {
URL url = new URL(urlPath);
stream = url.openConnection().getInputStream();
InputStreamReader reader = new InputStreamReader(stream);
fos = new FileOutputStream(output.getPath());
int next = -1;
while ((next = reader.read()) != -1) {
fos.write(next);
}
//Maybe I need to do something else here????
// Successful finished
result = Activity.RESULT_OK;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
publishResults(output.getName(), output.getAbsolutePath(), result);
}
}