[英]Renaming selected image from gallery
I'm trying to make an app where user selects an Image from gallery and can rename it to desired name, The code to get the Image from gallery is 我正在尝试制作一个用户可以从图库中选择图像并将其重命名为所需名称的应用,从图库中获取图像的代码是
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, RESULT_LOAD_IMG);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
try {
upload.setVisibility(View.INVISIBLE);
Image.setVisibility(View.VISIBLE);
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
Image.setImageBitmap(selectedImage);
Log.i("FileName is", fileName);
} catch (FileNotFoundException e){
e.printStackTrace();
Toast.makeText(getApplicationContext(),"Error Loading Image",Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(getApplicationContext(),"Please select an image",Toast.LENGTH_LONG).show();
}
}
I've seen some posts which use from
and to
to rename Images but I'm not able to understand how it works and how to set a desired name(which will be entered by user via an editText). 我看过一些文章,它们使用
from
和to
重命名Images,但是我不明白它是如何工作的以及如何设置所需的名称(用户将通过editText输入)。
Any help would be appreciated 任何帮助,将不胜感激
Here you can find your relevant solution of @Davewebb and you are confused in from to
在这里,您可以找到@Davewebb的相关解决方案,您对
from to
感到困惑
File sdcard = Environment.getExternalStorageDirectory();
File from = new File(sdcard,"from.txt");
File to = new File(sdcard,"to.txt");
from.renameTo(to);
from.txt
is your old name and new name will be here is to.txt
from.txt
是您的旧名称,新名称将在此处to.txt
for internal Storage, you can read a file like this and will do the same above process 对于内部存储,您可以读取这样的文件,并执行与上面相同的过程
private String readFileFromInternalStorage(){
ImageView imageView = (ImageView) findViewById(R.id.image);
ContextWrapper cw = new ContextWrapper(context);
//path to /data/data/yourapp/app_data/dirName
File directory = cw.getDir("dirName", Context.MODE_PRIVATE);
File mypath=new File(directory,"imagename.jpg");
ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageDrawable(Drawable.createFromPath(mypath.toString()));
}
Here is the code which may help you 这是可能对您有帮助的代码
File sdcard = Environment.getExternalStorageDirectory();
File from = new File(sdcard, "from.txt");
File to = new File(sdcard, "to.txt");
if (updateFileName(context, from, to))
Log.d("File Rename", "Successfully renamed");
else Log.d("File Rename", "Rename filed");
public static boolean updateFileName(Context mContext, File from, File to) {
if (from.renameTo(to)) {
removeMedia(mContext, from);
updateMediaInGallery(mContext, to);
return true;
} else {
return false;
}
}
public static void updateMediaInGallery(Context c, File f) {
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(f));
c.sendBroadcast(intent);
}
private static void removeMedia(Context c, File f) {
ContentResolver resolver = c.getContentResolver();
resolver.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media.DATA + "=?", new String[]{f.getAbsolutePath()});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.