Getting "DownloadManager.STATUS_SUCCESSFUL" even after download not successful (when insufficient space) - android

I am always getting COLUMN_STATUS as STATUS_SUCCESSFUL even it fails due to insufficient space:
#Override
public void onReceive(Context context, Intent intent) {
if (intent == null) {
Utils.log(TAG, "Intent is NULL");
return;
}
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
DownloadManager downloadManager = (DownloadManager)
context.getSystemService(Context.DOWNLOAD_SERVICE);
long referenceId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, -1L);
if (downloadManager != null) {
DownloadManager.Query dmQuery = new DownloadManager.Query();
dmQuery.setFilterById(referenceId);
try (Cursor cursor = downloadManager.query(dmQuery)) {
if (cursor != null && cursor.getCount() > 0) {
// The error is here
int columnIndex =cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (cursor.moveToFirst() && cursor.getInt(columnIndex) == DownloadManager.STATUS_SUCCESSFUL) {
} else {
Log.e(TAG, "fail");
}
}
} catch (Exception exception) {
Log.e(TAG, exception);
}
}
}
}

If your connection timeouts, you will get FAILED.
If it fails due to insufficient space, check the query columns COLUMN_TOTAL_SIZE_BYTES and COLUMN_BYTES_DOWNLOADED_SO_FAR to check if equal.
int bytes_total = cur.getInt(cur.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
int bytes_downloaded = cur.getInt(cur.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));

Related

How to access Device song from internal memoy

I am new to android development here and I want to open internal memory from my app. But it is open on all devices except Samsung device. It's showing fine on Motorola device, other phones and in Emulator also. I am getting null here: if (scanFolder == null) {return null;}
public List<Folder> loadInBackground() {
List<Folder> folderList = new ArrayList<>();
List<Song> songList = new ArrayList<>();
// Permission Check Runtime For M and above
if (PermissionChecker.checkCallingOrSelfPermission(getContext(), Manifest.permission.READ_EXTERNAL_STORAGE) == PermissionChecker.PERMISSION_GRANTED) {
FileExtensionFilter mFileExtensionFilter = new FileExtensionFilter(Constants.fileExtensions);
if (dir != null) {
File[] scanFolder = dir.listFiles(mFileExtensionFilter);
// Getting Null over here in Samsung Device scanFolder getting null
if (scanFolder == null) {
return null;
}
// Get Folder List
for (File aScanFolder : scanFolder) {
Folder folder = new Folder();
Cursor cursor = getContext().getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Audio.Media.DATA}, MediaStore.Audio.Media.DATA + " like ? ", new String[]{"%" + aScanFolder.getAbsolutePath() + "%"}, null);
if (cursor != null) {
int count = cursor.getCount();
if (count != 0) {
if (!aScanFolder.isDirectory()) {
String path = aScanFolder.getAbsolutePath();
Song song = Helper.getSongData(Extras.getInstance().getSongSortOrder(), getContext(), path);
songList.add(song);
}
if (!aScanFolder.getAbsolutePath().startsWith("/d")) {
Log.e("FolderLoader", "Path --> " + aScanFolder.getAbsolutePath());
folder.setFile(aScanFolder);
folder.setFileCount(count);
folder.setSongList(songList);
folderList.add(folder);
}
}
}
if (cursor != null) {
cursor.close();
}
}
Collections.sort(folderList, new Comparator<Folder>() {
#Override
public int compare(Folder f1, Folder f2) {
if ((f1.getFile().isDirectory() && f2.getFile().isDirectory()))
return f1.getFile().getName().compareToIgnoreCase(f2.getFile().getName());
else if (f1.getFile().isDirectory() && !f2.getFile().isDirectory())
return -1;
else if (!f1.getFile().isDirectory() && f2.getFile().isDirectory())
return 1;
else if (!f1.getFile().isDirectory() && !f2.getFile().isDirectory())
return f1.getFile().getName().compareToIgnoreCase(f2.getFile().getName());
else return 0;
}
});
if (!dir.getAbsolutePath().equals("/")) {
Folder folder = new Folder();
if (dir.getParentFile() != null) {
folder.setFile(dir.getParentFile());
Log.e("FolderLoader", dir.getParentFile().getAbsolutePath());
folderList.add(0, folder);
}
}
}
return folderList;
} else {
// Error Message
Log.d("Folder", "Permission not granted");
return Collections.emptyList();
}
}
I am getting this Screen UI in Samsung Device.
Getting All songs in Other device except Samsung Device

CursorWrapperInner: Cursor finalized without prior close()

I have built a rss news app. It is working fine upto some point with some bugs. Problem is content provider is not adding new News for some rss feeds and getting Cursor finalized without prior close() error in logs. I am refreshing the feed in background IntentService. Need to improve performance if possible.
private int refreshLocalFeed(Feeds feeds) throws RemoteException, OperationApplicationException {
LogMessage.d("feed refresh:", feeds.getCategoryId() + " : " + feeds.getName());
Call<RSSFeed2> call = api.loadOwnRssFeed(feeds.getUrl());
ContentResolver cr = MyApp.getAppContext().getContentResolver();
int success = 0;
try {
Response<RSSFeed2> response = call.execute();
RSSFeed2 rssFeed = response.body();
if (rssFeed != null) {
ArrayList<ContentProviderOperation> operations = new ArrayList<>();
Collections.reverse(rssFeed.getArticleList());
for (Article article : rssFeed.getArticleList()) {
Cursor c = null;
try {
c = cr.query(DbContract.EntryColumns.CONTENT_URI, new String[]{DbContract.EntryColumns._ID}, DbContract.EntryColumns.CATEGORYID + "=? AND " + DbContract.EntryColumns.LINK + "=?",
new String[]{feeds.getCategoryId(), article.getLink()}, null);
} catch (Exception e) {
e.printStackTrace();
}
ContentValues values = new ContentValues();
values.put(DbContract.EntryColumns.TITLE, article.getTitle());
values.put(DbContract.EntryColumns.DESCRIPTION, article.getDescription());
values.put(DbContract.EntryColumns.DATE, article.getPubDate());
values.put(DbContract.EntryColumns.AUTHOR, getString(R.string.app_name));
values.put(DbContract.EntryColumns.FETCH_DATE, System.currentTimeMillis());
values.put(DbContract.EntryColumns.CATEGORYID, feeds.getCategoryId());
values.put(DbContract.EntryColumns.GUID, article.getGuid());
String alternateImageUrl = getImageUrl(article.getDescription());
if (article.getThumbnail() != null && !article.getThumbnail().isEmpty()) {
values.put(DbContract.EntryColumns.IMAGE_URL, article.getThumbnail());
} else if (!alternateImageUrl.isEmpty()) {
values.put(DbContract.EntryColumns.IMAGE_URL, getImageUrl(article.getDescription()));
}
values.put(DbContract.EntryColumns.LINK, article.getLink());
if (c != null && c.getCount() > 0) {
operations.add(ContentProviderOperation.newUpdate(DbContract.EntryColumns.CONTENT_URI)
.withSelection(DbContract.EntryColumns._ID, new String[]{String.valueOf(c.getInt(c.getColumnIndexOrThrow(DbContract.EntryColumns._ID)))})
.withValues(values)
.build());
c.close();
} else {
operations.add(ContentProviderOperation.newInsert(DbContract.EntryColumns.CONTENT_URI)
.withValues(values)
.build());
}
}
ContentProviderResult[] results = cr.applyBatch(DbContract.AUTHORITY, operations);
for (ContentProviderResult result : results) {
if (result.uri != null) {
success++;
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return success;
}
edit
if (c != null) {
if (c.getCount() > 0) {
operations.add(ContentProviderOperation.newUpdate(DbContract.EntryColumns.CONTENT_URI)
.withSelection(DbContract.EntryColumns._ID, new String[]{String.valueOf(c.getInt(c.getColumnIndexOrThrow(DbContract.EntryColumns._ID)))})
.withValues(values)
.build());
}
c.close();
} else {
operations.add(ContentProviderOperation.newInsert(DbContract.EntryColumns.CONTENT_URI)
.withValues(values)
.build());
}

Scanning internal storage in android for all images

I am trying to scan internal storage for all the available images, but getting 0 results, where as, i store one image just before doing the scan, that image is "wallpaper.jpg".
Thanks in advance for any help.
boolean fileFlag= fileExistance("/wallpaper.jpg");
System.out.println(fileFlag); // this returns "true"
initMediaContentLevels(TAG, Images.Media.INTERNAL_CONTENT_URI, CoreConstants.DATATYPE_PHOTO, listener);
protected void initMediaContentLevels(final String tag, final Uri mediaUri, final int dataType, final DataManagerListener listener) {
LOG.i(TAG, "initMediaContentLevels("+tag+","+mediaUri+","+dataType+") mClassItemsTotal="+mClassItemsTotal);
//TODO if the data card is not mounted or missing then this returns 0 .. should test that sd card is available when app starts
String[] projection = {MediaStore.MediaColumns.DATA};
Cursor mediaExtCur = sContext.getContentResolver().query(
mediaUri,
projection,
null,
null,
null);
LOG.d(TAG, "initMediaContentLevels("+tag+","+mediaUri+","+dataType+") mediaExtCur="+mediaExtCur);
if (mediaExtCur!=null) {
int numItems = mediaExtCur.getCount(); //allows for chaining of class for one upload group (row of UI)
LOG.d(TAG, "initMediaContentLevels("+tag+") numItems="+numItems);
try {
if (numItems > 0) {
while (mediaExtCur.moveToNext()) {
String filepath = getString(mediaExtCur, MediaColumns.DATA);
File f = new File(filepath);
if (!f.exists() || !f.canRead()) {
LOG.e(TAG, "initMediaContentLevels("+tag+") file "+f.getAbsolutePath()+" "+(f.exists()?"is not readable":"does not exist"));
continue;
}
long fileSize = f.length();
if (exceedsFileSizeLimit(fileSize)) {
LOG.e(TAG, "initMediaContentLevels("+tag+") file is too large to announce "+filepath+": "+fileSize);
//files larger than the max are not counted
continue;
}
mBytesTotal += fileSize; //db sizes are unreliable
mClassItemsTotal++;
}
LOG.d(TAG, "initMediaContentLevels("+tag+") mBytesTotal="+mBytesTotal+", mClassItemsTotal="+mClassItemsTotal);
}
}
catch (Exception e) {
LOG.e(TAG,"problem getting media count "+e.toString());
}
finally {
if (!mediaExtCur.isClosed()) {
mediaExtCur.close();
}
}
}
LOG.d(TAG, "initMediaContentLevels("+tag+") listener="+listener);
listener.initialiseClassCount(dataType, mClassItemsTotal, mBytesTotal);
LOG.d(TAG, "initMediaContentLevels("+tag+") done");
}

Why I cant't get the video duration from MediaStore?

On one of my device, the following code can't make my video appear in Gallery:
File file = new File(path);
Uri uri = Uri.fromFile(file);
Intent scanFileIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
activity.sendBroadcast(scanFileIntent);
So I use scanFile explicitly:
android.media.MediaScannerConnection.scanFile(activity, new String[]{file.getAbsolutePath()},
new String[]{"video/" + mMimeType}, null);
When my video is xxx.mpeg4, the value of mMimeType is mp4, the result is that the video can appear in MediaStore but I can't get the duration later(the returned value is always 0). Need help on this.
public static long[] getVideoDetail(Context context, Uri uri) {
long[] result = new long[] {DEFAULT_VIDEO_FRAME_WIDTH, DEFAULT_VIDEO_FRAME_HEIGHT, -1};
if(uri == null || (!ContentResolver.SCHEME_CONTENT.equals(uri.getScheme()))) {
return result;
}
String[] projection = new String[] {MediaStore.Video.Media.RESOLUTION, MediaStore.Video.VideoColumns.DURATION};
Cursor cursor = null;
boolean success = false;
try {
cursor = context.getContentResolver().query(uri, projection, null, null, null);
if (cursor.moveToFirst()) {
String resolution = cursor.getString(0);
if(!StringUtils.isEmpty(resolution)) {
int index = resolution.indexOf('x');
result[0] = Integer.parseInt(resolution.substring(0, index));
result[1] = Integer.parseInt(resolution.substring(index + 1));
if(result[0] != 0 && result[1] != 0) {
success = true;
}
if(result[0] > result[1]) {
swap(result, 0, 1);
}
}
result[2] = cursor.getLong(1);
if(result[2] >= 0 && success) {
success = true;
} else {
success = false;
}
}
if (null != cursor) {
cursor.close();
}
}
catch (Exception e) {
// do nothing
} finally {
try {
if (null != cursor) {
cursor.close();
}
} catch (Exception e2) {
// do nothing
}
}
if (!success) {
try {
ContentResolver contentResolver = context.getContentResolver();
String selection = MediaStore.Images.Media._ID + "= ?";
String id = uri.getLastPathSegment();
String[] selectionArgs = new String[]{ id };
cursor = contentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, selection, selectionArgs, null);
if (cursor.moveToFirst()) {
String resolution = cursor.getString(0);
if(!StringUtils.isEmpty(resolution)) {
int index = resolution.indexOf('x');
result[0] = Integer.parseInt(resolution.substring(0, index));
result[1] = Integer.parseInt(resolution.substring(index + 1));
if(result[0] > result[1]) {
swap(result, 0, 1);
}
}
result[2] = cursor.getLong(1);
}
if (null != cursor) {
cursor.close();
}
} catch (Exception e) {
// do nothing
} finally {
try {
if (cursor != null) {
cursor.close();
}
} catch (Exception e) {
// do nothing
}
}
}
if(result[0] <= 0) {
result[0] = DEFAULT_VIDEO_FRAME_WIDTH;
}
if(result[1] <= 0) {
result[1] = DEFAULT_VIDEO_FRAME_HEIGHT;
}
return result;
}
As I see in your code you are requesting duration in your projection
String[] projection = new String[] {MediaStore.Video.Media.RESOLUTION, MediaStore.Video.VideoColumns.DURATION};
now you just need to retrieve it from the cursor like shown below:
long timeInMs = cursor.getLong(cursor.getColumnIndex(MediaStore.Video.VideoColumns.DURATION));
get help from MediaPlayer :
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(context, Uri.parse(uri));
} catch (IOException e) {
Log.d("-MS-","Cannot parse url");
e.printStackTrace();
}
int duration= mp.getDuration();
String[] projection = new String[] {MediaStore.Video.Media.RESOLUTION,duration};
I always get Duration by MediaPlayer.

Installing apk in android

Good day! I'm trying to install downloaded apk.I've uploaded it to
http://w194442.open.ge.tt/1/files/........?download
I download it by this code:
downloadUrl = urlStringChooser(myURL);
String downloadCompleteIntentName = DownloadManager.ACTION_DOWNLOAD_COMPLETE;
IntentFilter downloadCompleteIntentFilter = new
IntentFilter(downloadCompleteIntentName);
context = getApplicationContext();
context.registerReceiver(downloadCompleteReceiver, downloadCompleteIntentFilter);
String servicestring = Context.DOWNLOAD_SERVICE;
downloadmanager = (DownloadManager) getSystemService(servicestring);
Uri uri = Uri.parse(downloadUrl);
DownloadManager.Request request = new DownloadManager.Request(uri);
downloadID = downloadmanager.enqueue(request);
After that I get a broadcast signal from system that file downloaded :
private BroadcastReceiver downloadCompleteReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0L);
if (id != downloadID) {
private BroadcastReceiver downloadCompleteReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0L);
if (id != downloadID) {
Toast toast = Toast.makeText(getApplicationContext(), "downloadID FAIL", Toast.LENGTH_LONG);
toast.show();
return;
}
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(id);
Cursor cursor = downloadManager.query(query);
if (!cursor.moveToFirst()) {
Toast toast = Toast.makeText(getApplicationContext(), "cursor.moveToFirst FAIL", Toast.LENGTH_LONG);
toast.show();
return;
}
int statusIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL != cursor.getInt(statusIndex)) {
Toast toast = Toast.makeText(getApplicationContext(), "cDownloadManager.STATUS_SUCCESSFUL != cursor.getInt(statusIndex)", Toast.LENGTH_LONG);
toast.show();
return;
}
int uriIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI);
String downloadedPackageUriString = cursor.getString(uriIndex);
uri = Uri.parse(downloadedPackageUriString);
if(uri != null) {
Toast toast = Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_LONG);
toast.show();
installUpdate(uri);
}
}
};
return;
}
DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(id);
Cursor cursor = downloadManager.query(query);
if (!cursor.moveToFirst()) {
Toast toast = Toast.makeText(getApplicationContext(), "cursor.moveToFirst
FAIL", Toast.LENGTH_LONG);
toast.show();
return;
}
int statusIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL != cursor.getInt(statusIndex)) {
Toast toast = Toast.makeText(getApplicationContext(),
"cDownloadManager.STATUS_SUCCESSFUL != cursor.getInt(statusIndex)",
Toast.LENGTH_LONG);
toast.show();
return;
}
int uriIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI);
String downloadedPackageUriString = cursor.getString(uriIndex);
uri = Uri.parse(downloadedPackageUriString);
if(uri != null) {
Toast toast = Toast.makeText(getApplicationContext(), "Success",
Toast.LENGTH_LONG);
toast.show();
installUpdate(uri);
}
}
};
Works fine for now. Then I call installUpdate(uri); to install it.
Intent promptInstall = new Intent(Intent.ACTION_VIEW);
promptInstall.setClass(context, MyActivity.class);
promptInstall.setDataAndType(goodUri,"application/vnd.android.package-archive");
promptInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.d(TAG, "Before startActivity ");
startActivity(promptInstall);
And nothing happens. Logcat is
09-23 05:30:07.531 9492-9492/regulatory.securitycode.com.vpnupdate D/Updater﹕ Before
startActivity
09-23 05:30:07.556 9492-9492/regulatory.securitycode.com.vpnupdate E/dalvikvm﹕
dvmPauseGc(AppLaunch) called - cookie=0xf692 (f=0x1)
09-23 05:30:07.566 9492-9492/regulatory.securitycode.com.vpnupdate
E/MoreInfoHPW_ViewGroup﹕ Parent view is not a TextView
09-23 05:30:07.611 9492-9492/regulatory.securitycode.com.vpnupdate D/mali_winsys﹕
new_window_surface returns 0x3000
09-23 05:30:07.651 9492-9492/regulatory.securitycode.com.vpnupdate E/dalvikvm﹕
dvmResumeGc(0xf692, 0) called (f=0x1)
What do I do wrong?
Have you checked the "Install from Unknown sources" option in the Security Settings?

Categories

Resources