如何使用下载管理器
在内部存储中保存图像或mp3文件代码:
public void StartDownload(String path)
{
ContextWrapper cw = new ContextWrapper(context);
File directory = cw.getDir("channel" + cId, Context.MODE_PRIVATE);
if (!directory.exists()) {
directory.mkdir();
}
DownloadManager mgr = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(path);
DownloadManager.Request request = new DownloadManager.Request(
downloadUri);
String imgnm = path.substring(path.lastIndexOf("/") + 1);
startdownloadurl = directory.getAbsolutePath()+"/";
System.out.println(" directory " + startdownloadurl);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false).setTitle(imgnm)
.setDescription("Downloading...")
.setVisibleInDownloadsUi(true)
.setDestinationInExternalPublicDir(startdownloadurl, imgnm);
mgr.enqueue(request);
}
我试图将下载的图像或mp3文件存储在我的内部存储中 但效果不好 必需的路径是“data / data / packagename / app_channel1 / image1.jpg”
答案 0 :(得分:14)
只有您自己的应用可以Access
到应用internal storage
默认的Android内置下载管理器无法访问您的应用内部存储空间,因此您无法下载内部存储空间。
<强>解决方案:强>
在sd card
临时文件中下载文件,下载完成后register a receive
,然后将文件从external
复制到internal storage.
完整代码:
public class MainActivity extends Activity {
private long enqueue;
private DownloadManager dm;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, 0);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(enqueue);
Cursor c = dm.query(query);
if (c.moveToFirst()) {
int columnIndex = c
.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c
.getInt(columnIndex)) {
ImageView view = (ImageView) findViewById(R.id.imageView1);
String uriString = c
.getString(c
.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
Uri a = Uri.parse(uriString);
File d = new File(a.getPath());
// copy file from external to internal will esaily avalible on net use google.
view.setImageURI(a);
}
}
}
}
};
registerReceiver(receiver, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
public void onClick(View view) {
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse("http://www.vogella.de/img/lars/LarsVogelArticle7.png")).setDestinationInExternalPublicDir("/Sohail_Temp", "test.jpg");
enqueue = dm.enqueue(request);
}
public void showDownload(View view) {
Intent i = new Intent();
i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
startActivity(i);
}
}
<强>布局:强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Start Download"></Button>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="showDownload"
android:text="View Downloads"></Button>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image_1"></ImageView>
</LinearLayout>
<强>权限:强>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />