gpt4 book ai didi

android - 如何使用 android 中的下载管理器将下载的图像存储在内部存储中

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:28:39 27 4
gpt4 key购买 nike

如何使用下载管理器将图像或 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”

最佳答案

只有您自己的应用程序可以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" />

关于android - 如何使用 android 中的下载管理器将下载的图像存储在内部存储中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38563474/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com