【问题标题】:Renaming selected image from gallery从图库中重命名所选图像
【发布时间】:2019-04-18 03:26:04
【问题描述】:

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

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 输入) .

任何帮助将不胜感激

【问题讨论】:

  • 如果图像在同一个文件夹中,你将如何处理重命名?

标签: android imageview file-rename


【解决方案1】:

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

【讨论】:

  • 非常感谢,但是如果用户从内部存储中选择文件怎么办?它会以同样的方式工作吗?
  • 不,我认为你不需要设置getExternalStorageDirectory
  • 那么我们必须传入什么而不是'sdcard'? 'imageUri'?
【解决方案2】:

这里是可以帮助你的代码

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-09
    相关资源
    最近更新 更多