i am getting error when i try to download this file in Api 23.it works well in version<23.I have seen similar questions like this,but none of the solution worked for me. i am getting exception for read write access ..How to give read and write access permission in marshmallow..?
I have given read and write permission in android manifest like this.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Download.java
import java.io.File;
import android.app.Activity;
import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class Download extends Activity {
private DownloadManager mgr=null;
private long lastDownload=-1L;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download);
mgr=(DownloadManager)getSystemService(DOWNLOAD_SERVICE);
registerReceiver(onComplete,
new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
registerReceiver(onNotificationClick,
new IntentFilter(DownloadManager.ACTION_NOTIFICATION_CLICKED));
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(onComplete);
unregisterReceiver(onNotificationClick);
}
public void startDownload(View v) {
Uri uri=Uri.parse("http://oursite/pictures/image.jpg");
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
.mkdirs();
lastDownload=
mgr.enqueue(new DownloadManager.Request(uri)
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |
DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle("Pictures")
//.setDescription("Something useful. No, really.")
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
"aadhar.jpg"));
v.setEnabled(false);
//findViewById(R.id.query).setEnabled(true);
}
public void queryStatus(View v) {
Cursor c=mgr.query(new DownloadManager.Query().setFilterById(lastDownload));
if (c==null) {
Toast.makeText(this, "Update not found!", Toast.LENGTH_LONG).show();
}
else {
c.moveToFirst();
Log.d(getClass().getName(), "COLUMN_ID: "+
c.getLong(c.getColumnIndex(DownloadManager.COLUMN_ID)));
Log.d(getClass().getName(), "COLUMN_BYTES_DOWNLOADED_SO_FAR: "+
c.getLong(c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)));
Log.d(getClass().getName(), "COLUMN_LAST_MODIFIED_TIMESTAMP: "+
c.getLong(c.getColumnIndex(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP)));
Log.d(getClass().getName(), "COLUMN_LOCAL_URI: "+
c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)));
Log.d(getClass().getName(), "COLUMN_STATUS: "+
c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)));
Log.d(getClass().getName(), "COLUMN_REASON: "+
c.getInt(c.getColumnIndex(DownloadManager.COLUMN_REASON)));
Toast.makeText(this, statusMessage(c), Toast.LENGTH_LONG).show();
}
}
public void viewLog(View v) {
startActivity(new Intent(DownloadManager.ACTION_VIEW_DOWNLOADS));
}
private String statusMessage(Cursor c) {
String msg="???";
switch(c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS))) {
case DownloadManager.STATUS_FAILED:
msg="Update failed!";
break;
case DownloadManager.STATUS_PAUSED:
msg="Update paused!";
break;
case DownloadManager.STATUS_PENDING:
msg="Update pending!";
break;
case DownloadManager.STATUS_RUNNING:
msg="Update in progress!";
break;
case DownloadManager.STATUS_SUCCESSFUL:
msg="Update complete!";
break;
default:
msg="Update software is nowhere in sight";
break;
}
return(msg);
}
BroadcastReceiver onComplete=new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
findViewById(R.id.start).setEnabled(true);
}
};
BroadcastReceiver onNotificationClick=new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
Toast.makeText(ctxt, "Ummmm...hi!", Toast.LENGTH_LONG).show();
}
};
}