我正在创建一个类似app的图库,我可以选择将图像移动到目标目录。正在移动图像并删除源图像,但新文件夹未在我的应用程序中列出。在我的默认图库文件夹中,可以看到我移动的图片。
public void moveToFav(String filePath){
boolean renameBoolean = false;
File from = new File(filePath);
String revName = new StringBuilder(filePath).reverse().toString();
String array2[]= revName.split("/", 2);
String realName = new StringBuilder(array2[0]).reverse().toString();
File to = new File(Function.FAV_PATH+"/"+realName);
if (from.renameTo(to)){
getContentResolver().delete(MediaStore.Files.getContentUri("external"),
MediaStore.MediaColumns.DATA + "=?", new String[]{filePath});
LoadAlbum loadAlbum = new LoadAlbum(FullScreenImageActivity.this);
loadAlbum.execute();
}else{
Toast.makeText(FullScreenImageActivity.this,"Please retry after sometime..",Toast.LENGTH_LONG).show();
}
}
这是我的AsyncTask,我在那里获取所有图像:
package com.apps.sumukha.galleri.AsyncTasks;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.MergeCursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore;
import com.apps.sumukha.galleri.Activities.SplashScreen;
import com.apps.sumukha.galleri.MainActivity;
import com.apps.sumukha.galleri.Utils.Function;
import com.apps.sumukha.galleri.Utils.MapComparator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
/**
* Created by sumukha on 4/4/18.
*/
public class LoadAlbum extends AsyncTask<String, Void, String> {
ArrayList<HashMap<String, String>> albumList = new ArrayList<HashMap<String, String>>();
Context context;
public LoadAlbum(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
albumList.clear();
System.out.println("*****###### cAME to async");
}
protected String doInBackground(String... args) {
String xml = "";
String path = null;
String album = null;
String timestamp = null;
String countPhoto = null;
Uri uriExternal = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Uri uriInternal = android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI;
String[] projection = { MediaStore.MediaColumns.DATA,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME, MediaStore.MediaColumns.DATE_MODIFIED };
Cursor cursorExternal = context.getContentResolver().query(uriExternal, projection, "_data IS NOT NULL) GROUP BY (bucket_display_name",
null, null);
Cursor cursorInternal = context.getContentResolver().query(uriInternal, projection, "_data IS NOT NULL) GROUP BY (bucket_display_name",
null, null);
Cursor cursor = new MergeCursor(new Cursor[]{cursorExternal,cursorInternal});
while (cursor.moveToNext()) {
path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME));
timestamp = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATE_MODIFIED));
countPhoto = Function.getCount(context, album);
albumList.add(Function.mappingInbox(album, path, timestamp, Function.converToTime(timestamp), countPhoto));
}
cursor.close();
Collections.sort(albumList, new MapComparator(Function.KEY_TIMESTAMP, "dsc")); // Arranging photo album by timestamp decending
return xml;
}
@Override
protected void onPostExecute(String xml) {
System.out.println("****#### post report "+albumList.toString());
Intent intent = new Intent(context,MainActivity.class);
intent.putExtra("albumlist",albumList);
context.startActivity(intent);
// AlbumAdapter adapter = new AlbumAdapter(MainActivity.this, albumList);
// galleryGridView.setAdapter(adapter);
// galleryGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
// public void onItemClick(AdapterView<?> parent, View view,
// final int position, long id) {
// Intent intent = new Intent(MainActivity.this, AlbumActivity.class);
// intent.putExtra("name", albumList.get(+position).get(Function.KEY_ALBUM));
// startActivity(intent);
// }
// });
}
}