重命名图库中的所选图像

时间:2018-11-15 04:30:07

标签: android imageview file-rename

我正在尝试制作一个用户可以从图库中选择图片并可以将其重命名为所需名称的应用, 从图库获取图像的代码是

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();
    }
}

我看过一些使用fromto重命名图片的帖子,但我无法理解它的工作原理以及如何设置所需的名称(由用户输入通过editText)。

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:0)

Here,您可以找到有关@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是您的旧名字,新名字将在这里to.txt

对于内部存储,您可以读取这样的文件,并执行与上述相同的过程

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()));
}

答案 1 :(得分:0)

以下代码可能对您有帮助

    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()});
    }