You have to wait until the mediascanner posts a callback when you it
is done.
I wrote a wrapper around this behavior, which you can call by this
example:
// msListener's 'ready(...)' method is called when the imgUri has
been scanned completely.
MediaScannerNotifier.run(getApplicationContext(), new Handler(),
imgUri, msListener);
=======================
public class MediaScannerNotifier implements
MediaScannerConnectionClient {
private Context mContext;
private Handler mHandler;
private MediaScannerConnection mConnection;
private String mPath;
private String mMimeType;
private MediaScannerNotifierListener mCallback;
public interface MediaScannerNotifierListener {
public void ready(String path, Uri uri);
}
public static MediaScannerNotifier run(Context context, Handler
handler, Uri imgUri, MediaScannerNotifierListener callback) {
android.database.Cursor cur = null;
try {
cur = context.getContentResolver().query( imgUri,
new String[]
{ MediaStore.Images.ImageColumns.DATA },
null, null, null);
String filePath = null;
if (cur != null && cur.moveToNext()) {
filePath = cur.getString(0);
}
if (filePath != null) {
return new MediaScannerNotifier(context,
handler, filePath, "image/
jpeg", callback);
}
} catch (Exception e) {
// TODO show error message.
e.printStackTrace();
}
finally {
if (cur != null) {
cur.close();
}
}
return null;
}
private MediaScannerNotifier(Context context, Handler handler,
String path, String mimeType, MediaScannerNotifierListener callback)
{
mContext = context;
mPath = path;
mMimeType = mimeType;
mConnection = new MediaScannerConnection(mContext, this);
mConnection.connect();
mHandler = handler;
mCallback = callback;
}
public void onMediaScannerConnected() {
mConnection.scanFile(mPath, mMimeType);
}
public void onScanCompleted(final String path, final Uri uri) {
try {
if (mCallback != null) {
mHandler.post(new Runnable() {
public void run() {
mCallback.ready(path, uri);
}
});
}
}
finally {
mConnection.disconnect();
mContext = null;
}
}
}
=======================
On May 6, 6:11 pm, "L'\\tty" <[email protected]> wrote:
> Thanx for the hint! Could you possibly be so kind to post me the code
> for scanning a file manually. I tried it but anyhow, I get an
> exception because it seems that I am not connected to the Media
> Scanner service although I called the connect() method.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---