android file getname,android - 不推荐使用DownloadManager COLUMN_LOCAL_FILENAME - 堆栈内存溢出...

我通过解决这个问题 DownloadManager.COLUMN_LOCAL_URI 而不是 DownloadManager.COLUMN_LOCAL_FILENAME

DownloadManager.COLUMN_LOCAL_URI返回包含“file://”的文件路径,因此您必须使用downloadFilePath = downloadFilePath.replace("file://","");将其从downloadFilePath排除downloadFilePath = downloadFilePath.replace("file://","");

以下是此问题的一行解决方案:

String downloadFilePath = (c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))).replace("file://","");

检查下面的DownloadManager完整代码:

DownloadFinishedReceiver.java

public class DownloadFinishedReceiver extends BroadcastReceiver {

@Override

public void onReceive(final Context context, Intent intent) {

String action = intent.getAction();

if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action) && intent.getExtras()!=null) {

Bundle extras = intent.getExtras();

DownloadManager.Query q = new DownloadManager.Query();

long downloadId = extras.getLong(DownloadManager.EXTRA_DOWNLOAD_ID);

q.setFilterById(downloadId);

Cursor c = ((DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE)).query(q);

if (c.moveToFirst()) {

int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));

if (status == DownloadManager.STATUS_SUCCESSFUL) {

String downloadFilePath = (c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI))).replace("file://","");

String downloadTitle = c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE));

c.close();

Log.e("DownloadPath", downloadFilePath); // Print DownloadPath in Logcat

Log.e("DownloadTitle", downloadTitle); // Print DownloadTitle in Logcat

} else if (status == DownloadManager.STATUS_FAILED) {

removeTempOnFailure(context, downloadId);

}

}

}

}

// Remove temp/cache data

private void removeTempOnFailure(Context con, long downloadId) {

File cacheFileDir = new File(con.getCacheDir().getAbsolutePath());

for (File f : cacheFileDir.listFiles()) {

if (f.getName().contains(String.valueOf(downloadId))) {

f.delete();

break;

}

}

}

}

在AndroidMenifest.xml文件中注册BroadcastReceiver:

android:name="com.example.receiver.DownloadFinishedReceiver"

android:exported="true"

android:process=".downloadFinished">

将以下方法放在Activity中并传递适当的参数:

/**

*

* @param downloadUrl -> Your file download url

* @param downloadTitle -> Your file title to display in download manager

* @param fileName -> filename with extension like music.mp3 it will store in download folder

* @param hide -> true to hide downloadmanager in status bar, false to display it

* @return -> it will return downloadId

*/

private long downloadFromUrl(String downloadUrl, String downloadTitle, String fileName, boolean hide) {

Uri uri = Uri.parse(downloadUrl);

DownloadManager.Request request = new DownloadManager.Request(uri);

request.setTitle(downloadTitle);

if (hide) {

request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);

request.setVisibleInDownloadsUi(false);

} else

request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);

DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

return manager != null ? manager.enqueue(request) : 0;

}

如果您在上面的方法中传递hide = true,则必须在AndroidManifext.xml添加以下权限

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值