I have for example button and when user click on it I want to find out the download history by DownloadManager, but problem is that my cursor is empty, it never goes to condition if(c.moveToFirst()), it always skip this condition.
DownloadManager downloadMgr = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterByStatus(DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PENDING|DownloadManager.STATUS_RUNNING|DownloadManager.STATUS_SUCCESSFUL);
Cursor c = downloadMgr.query(query);
if(c==null) {
//
}
else {
if(c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = c.getInt(columnIndex);
if(status == DownloadManager.STATUS_RUNNING){
//do something
}
}
}
I also tried this code (by imran khan) but still same problem:
DownloadManager.Query query = null;
Cursor c = null;
DownloadManager downloadManager = null;
downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
query = new DownloadManager.Query();
query.setFilterByStatus(DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PENDING|DownloadManager.STATUS_RUNNING|DownloadManager.STATUS_SUCCESSFUL);
c = downloadManager.query(query);
if(c.moveToFirst()) {
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
switch(status) {
case DownloadManager.STATUS_PAUSED:
break;
case DownloadManager.STATUS_PENDING:
break;
case DownloadManager.STATUS_RUNNING:
break;
case DownloadManager.STATUS_SUCCESSFUL:
break;
case DownloadManager.STATUS_FAILED:
break;
}
}
Related
I'm working on a feature where the user can take a picture and choose from the gallery. This is basically where it starts and goes on to save the images in db.
private void showPictureDialog(){
AlertDialog.Builder pictureDialog = new AlertDialog.Builder(this);
pictureDialog.setTitle("Select Action");
String[] pictureDialogItems = {
"Select photo from gallery",
"Capture photo from camera" };
pictureDialog.setItems(pictureDialogItems,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
choosePhotoFromGallary();
break;
case 1:
takePhotoFromCamera();
break;
}
}
});
pictureDialog.show();
}
However, I want to make the user experience better. I want to skip the dialog where the user selects one of the options (from gallery or camera) and instead show the gallery in camera intent. Something similar to this:
I hope you get my point. Thanks :)
Get all image
public List<File> getAllShownImagesPath(Context context) {
//get all images
String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_ADDED, MediaStore.Images.Media.SIZE};
List<File> result = new ArrayList<>();
File f = null;
final Cursor cursor = context.getContentResolver().
query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, // Specify the provider
columns, // The columns we're interested in
null, // A WHERE-filter query
null, // The arguments for the filter-query
MediaStore.Images.Media.DATE_ADDED + " DESC"
);
if (cursor != null) {
cursor.moveToFirst();
for (int r = 0; r < cursor.getCount(); r++, cursor.moveToNext()) {
int i = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE));
//int l = cursor.getString(1).length();
final int image_path_col = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
if (i > 0) {
f = new File(cursor.getString(image_path_col));
if (f.length() > 0) {
result.add(f);
}
}
}
cursor.close();
}
return result;
}
Add all image into recyclerview or listview
I want to download a zip file using download manager.Using this code it shows downloading file in notification and later shows download failed. I gave permissions such as read and write external directory.My code is as follows:
if(DownloadTask.readAndWriteExternalStorage(context)){
downloadManager=
(DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri=Uri.parse("url of zip");
DownloadManager.Request request= new
DownloadManager.Request(uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE|
DownloadManager.Request.NETWORK_WIFI);
request.setVisibleInDownloadsUi(true);
request.setTitle("Example");
request.setDescription("Downloading a very large zip");
request.setAllowedOverRoaming(true);
request.setMimeType("df.zip");
request.allowScanningByMediaScanner();
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
"df.zip");
request.setNotificationVisibility(DownloadManager.
Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
Long reference=downloadManager.enqueue(request);
}
I solved the issue with this:
DownloadManager downloadManager;
private int count = 0;
private long Zip_DownloadId,imid;
Uri uri =
Uri.parse("http://website//"+rowItems.get(i).get("Attachments"));
if ( uri.toString().endsWith(".jpg")) {
imid=DownloadData(uri, view);
Check_Image_Status(imid);
} else if (uri.toString().endsWith(".png")) {
imid=DownloadData(uri, view);
Check_Image_Status(imid);
} else if (uri.toString().endsWith(".jpeg")) {
imid=DownloadData(uri, view);
Check_Image_Status(imid);
} else if (uri.toString().endsWith(".zip")) {
Zip_DownloadId = DownloadData(uri, view);
Check_Zip_Status(Zip_DownloadId);
}
Toast.makeText(getApplicationContext(),uri.toString(),Toast.LENGTH_LONG).show();
}
});
downloadReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//check if the broadcast message is for our Enqueued download
long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (referenceId == imid) {
Toast toast = Toast.makeText(SupportTicketViewActivity.this,
"Image Download Complete", Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 25, 400);
toast.show();
} else if (referenceId == Zip_DownloadId) {
Toast toast = Toast.makeText(SupportTicketViewActivity.this,
"Zip Download Complete", Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 25, 400);
toast.show();
}
}
};
return view;
}
private void Check_Zip_Status(long Zip_DownloadId) {
DownloadManager.Query ZipDownloadQuery = new DownloadManager.Query();
//set the query filter to our previously Enqueued download
ZipDownloadQuery.setFilterById(Zip_DownloadId);
//Query the download manager about downloads that have been requested.
Cursor cursor = downloadManager.query(ZipDownloadQuery);
if (cursor.moveToFirst()) {
DownloadStatus(cursor, Zip_DownloadId);
}}
private void Check_Image_Status(long imid) {
DownloadManager.Query ImageDownloadQuery = new DownloadManager.Query();
//set the query filter to our previously Enqueued download
ImageDownloadQuery.setFilterById(imid);
//Query the download manager about downloads that have been requested.
Cursor cursor = downloadManager.query(ImageDownloadQuery);
if(cursor.moveToFirst()){
DownloadStatus(cursor, imid);
}
}
private void DownloadStatus(Cursor cursor, long DownloadId) {
int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = cursor.getInt(columnIndex);
//column for reason code if the download failed or paused
int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
int reason = cursor.getInt(columnReason);
//get the download filename
int filenameIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME);
String filename = cursor.getString(filenameIndex);
String statusText = "";
String reasonText = "";
switch(status){
case DownloadManager.STATUS_FAILED:
statusText = "STATUS_FAILED";
switch(reason){
case DownloadManager.ERROR_CANNOT_RESUME:
reasonText = "ERROR_CANNOT_RESUME";
break;
case DownloadManager.ERROR_DEVICE_NOT_FOUND:
reasonText = "ERROR_DEVICE_NOT_FOUND";
break;
case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
reasonText = "ERROR_FILE_ALREADY_EXISTS";
break;
case DownloadManager.ERROR_FILE_ERROR:
reasonText = "ERROR_FILE_ERROR";
break;
case DownloadManager.ERROR_HTTP_DATA_ERROR:
reasonText = "ERROR_HTTP_DATA_ERROR";
break;
case DownloadManager.ERROR_INSUFFICIENT_SPACE:
reasonText = "ERROR_INSUFFICIENT_SPACE";
break;
case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
reasonText = "ERROR_TOO_MANY_REDIRECTS";
break;
case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
reasonText = "ERROR_UNHANDLED_HTTP_CODE";
break;
case DownloadManager.ERROR_UNKNOWN:
reasonText = "ERROR_UNKNOWN";
break;
}
break;
case DownloadManager.STATUS_PAUSED:
statusText = "STATUS_PAUSED";
switch(reason){
case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
reasonText = "PAUSED_QUEUED_FOR_WIFI";
break;
case DownloadManager.PAUSED_UNKNOWN:
reasonText = "PAUSED_UNKNOWN";
break;
case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
reasonText = "PAUSED_WAITING_FOR_NETWORK";
break;
case DownloadManager.PAUSED_WAITING_TO_RETRY:
reasonText = "PAUSED_WAITING_TO_RETRY";
break;
}
break;
case DownloadManager.STATUS_PENDING:
statusText = "STATUS_PENDING";
break;
case DownloadManager.STATUS_RUNNING:
statusText = "STATUS_RUNNING";
break;
case DownloadManager.STATUS_SUCCESSFUL:
statusText = "STATUS_SUCCESSFUL";
reasonText = "Filename:\n" + filename;
break;
}
if (DownloadId == Zip_DownloadId) {
Toast toast = Toast.makeText(SupportTicketViewActivity.this,
"Zip Download Status:" + "\n" + statusText + "\n" +
reasonText,
Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 25, 400);
toast.show();
} else {
Toast toast = Toast.makeText(SupportTicketViewActivity.this,
"Image Download Status:" + "\n" + statusText + "\n" +
reasonText,
Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 25, 400);
toast.show();
// Make a delay of 3 seconds so that next toast (Music Status) will not merge with this one.
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
}
}, 3000);
}
}
}
private long DownloadData(Uri uri, View view) {
long downloadReference;
downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(uri);
//Setting title of request
request.setTitle("Data Download");
//Setting description of request
request.setDescription("Android Data download using DownloadManager.");
//Set the local destination for the downloaded file to a path within the application's external files directory
if(uri.toString().endsWith(".jpg")) {
request.setDestinationInExternalFilesDir(SupportTicketViewActivity.this, Environment.DIRECTORY_DOWNLOADS, "AndroidTutorialPoint.jpg");
}else if (uri.toString().endsWith(".zip")){
request.setDestinationInExternalFilesDir(SupportTicketViewActivity.this, Environment.DIRECTORY_DOWNLOADS, "AndroidTutorialPoint.zip");
}else if(uri.toString().endsWith(".png")) {
request.setDestinationInExternalFilesDir(SupportTicketViewActivity.this, Environment.DIRECTORY_DOWNLOADS, "AndroidTutorialPoint.png");
} else if(uri.toString().endsWith(".jpeg")) {
request.setDestinationInExternalFilesDir(SupportTicketViewActivity.this, Environment.DIRECTORY_DOWNLOADS, "AndroidTutorialPoint.jpeg");
} //Enqueue download and save the referenceId
downloadReference = downloadManager.enqueue(request);
return downloadReference;
}
I don't know why but I get always STATUS_PENDING even if file is downloading and it must be STATUS_RUNNING.
If use an recursive method with runnable with delay, after couple seconds again and again on the same id I got the right status. Any ideas?
The code for obtaining status is:
public static void checkStatus(long downloadId) {
DownloadManager.Query query = new DownloadManager.Query();
Cursor cursor = manager.query(query.setFilterById(downloadId));
if(cursor.moveToFirst()){
int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
switch (status) {
case DownloadManager.STATUS_PENDING:
Log.d("status", "STATUS_PENDING");
//here you can set your TIMEOUT solution
break;
case DownloadManager.STATUS_PAUSED:
Log.d("status", "STATUS_PAUSED");
break;
case DownloadManager.STATUS_RUNNING:
Log.d("status", "STATUS_RUNNING");
break;
case DownloadManager.STATUS_SUCCESSFUL:
Log.d("status", "STATUS_SUCCESSFUL");
break;
case DownloadManager.STATUS_FAILED:
Log.d("status", "STATUS_FAILED");
break;
}
}
}
I am working with android.I am trying to get the status of downloading using download manager.But it does not work. I used the following code
DownloadManager.Query query = null;
Log.i("service1", "blah");
Cursor c = null;
DownloadManager downloadManager = null;
downloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
Log.i("service2", "blah");
query = new DownloadManager.Query();
if(query!=null) {
Log.i("service3", "blah");
query.setFilterByStatus(DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PAUSED|DownloadManager.STATUS_SUCCESSFUL|
DownloadManager.STATUS_RUNNING|DownloadManager.STATUS_PENDING);
} else {
Log.i("service4", "blah");
return;
}
c = downloadManager.query(query);
if(c.moveToFirst()) {
Log.i("service5", "blah");
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
Log.i("service6", "blah");
switch(status) {
case DownloadManager.STATUS_PAUSED:
break;
case DownloadManager.STATUS_PENDING:
break;
case DownloadManager.STATUS_RUNNING:
Toast.makeText(this, "Download started", Toast.LENGTH_SHORT).show();
break;
case DownloadManager.STATUS_SUCCESSFUL:
Toast.makeText(this, "Download completed", Toast.LENGTH_SHORT).show();
break;
case DownloadManager.STATUS_FAILED:
break;
}
}
It does not enter into if(c.moveToFirst()). Or any other way to get the download status in Android?
I use DownloadManager for getting status of downloading, but it still doesn't work, it never jumps to condition if(c.moveToFirst()) and I don't know why. Could anybody help me, please?
private final BroadcastReceiver myReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(Intent.ACTION_SCREEN_OFF.equals(action)) {
DownloadManager downloadMgr = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterByStatus(DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PENDING|DownloadManager.STATUS_RUNNING|DownloadManager.STATUS_SUCCESSFUL);
Cursor c = downloadMgr.query(query);
if(c==null) {
//
}
else {
if(c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
int status = c.getInt(columnIndex);
if(status == DownloadManager.STATUS_RUNNING){
//do something
}
}
}
}
}
};
Here are few link refer it.
LINK1
LINK2
sample code is below::
DownloadManager.Query query = null;
Cursor c = null;
DownloadManager downloadManager = null;
downloadManager = (DownloadManager)getSystemService(Context.DOWNLOAD_SERVICE);
query = new DownloadManager.Query();
if(query!=null) {
query.setFilterByStatus(DownloadManager.STATUS_FAILED|DownloadManager.STATUS_PAUSED|DownloadManager.STATUS_SUCCESSFUL|
DownloadManager.STATUS_RUNNING|DownloadManager.STATUS_PENDING);
} else {
return;
}
c = downloadManager.query(query);
if(c.moveToFirst()) {
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
switch(status) {
case DownloadManager.STATUS_PAUSED:
break;
case DownloadManager.STATUS_PENDING:
break;
case DownloadManager.STATUS_RUNNING:
break;
case DownloadManager.STATUS_SUCCESSFUL:
break;
case DownloadManager.STATUS_FAILED:
break;
}
}
This is how I do it
//broadcastReceiver
private val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
//call notify off NotificationManager passing in the id & notification
notificationManager.apply {
notify(NOTIFICATION_ID, createNotification())
}
//DownloadManager.Query() is used to filter DownloadManager queries
val query = DownloadManager.Query()
query.setFilterById(id)
val cursor = downloadManager.query(query)
if (cursor.moveToFirst()){
val status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))
when (status) {
DownloadManager.STATUS_SUCCESSFUL ->{
}
DownloadManager.STATUS_FAILED -> {
}
}
}
}
}