Preventing Media Scanner from Scanning JPGs Downloaded with the DownloadManager on Android? (Developer) -
my application downloads jpgs website using downloadmanager , store them in /sdcard/android/name.app/ directory used in several fragments in application. download works great however, jpgs visible in gallery application, don't want since assets application. cannot figure out how have them not show in gallery application. tried adding .nomedia files directories created. appreciated!
downloadmanager mydownloadmanager = (downloadmanager) myactivity.getsystemservice(context.download_service); request myrequest = new request(uri.parse(episodeartlink)); myrequest.setnotificationvisibility(request.visibility_hidden); myrequest.setvisibleindownloadsui(false); myrequest.setdestinationinexternalfilesdir(myactivity, environment.directory_downloads, myfilename); mydownloadmanager.enqueue(myrequest);
the issues seem caused because mediascanner adding images system database though have not asked to.
i have created test app demonstrates problem. if click download button google logo show in app. if go gallery app should see google logo in gallery named download. if go settings -> apps , clear data gallery , media storage google logo gone , won't show again in gallery because file under /sdcard/android/data/ protected .nomedia file. can go /sdcard/android/data/com.example.downloadmanagertest/files/download/ verify images still on phone. after reboot images don't show in gallery if clear data gallery , media storage. have tested on both nexus 4 , nexus 7 running 4.2.2.
source: https://dl.dropboxusercontent.com/u/114414/downloadmanagertest.zip
solution
the media scanner sooner or later scan media card media , index them. first intuitive step trick scanner not knowing file media file. can achieved saving images without extension. easy solution append notkevin end of filename. simply:
instead of
myrequest.setdestinationinexternalfilesdir(this, environment.directory_downloads, uri.parse(downloadlink).getlastpathsegment());
use this
string filename = uri.parse(downloadlink).getlastpathsegment() + "notkevin"; myrequest.setdestinationinexternalfilesdir(this, environment.directory_downloads, filename);
however not sufficient in case since using downloadmanager (just guess). media scanner somehow knows downloaded item , fact image and, thus, indexes it. disallow it, use setmimetype
this:
myrequest.setmimetype("application/octet-stream");
ps: there ways change extension other adding or name end of filename. example, hash filename
string filename = hash(uri.parse(downloadlink).getlastpathsegment()); public string hash(string victim) throws nosuchalgorithmexception { messagedigest md = messagedigest.getinstance("sha1"); md.reset(); byte[] buffer = victim.getbytes(); md.update(buffer); byte[] digest = md.digest(); stringbuilder hexstr = new stringbuilder(); (int = 0; < digest.length; i++) { hexstr.append(integer.tostring( ( digest[i] & 0xff ) + 0x100, 16).substring( 1 )); } return hexstr.tostring(); }
Comments
Post a Comment