【问题标题】:Copy existing png file and rename programmatically复制现有的 png 文件并以编程方式重命名
【发布时间】:2017-06-24 03:30:19
【问题描述】:

我在 SD 卡上的“电影”文件夹中有一个 png 文件。我想在同一个文件夹中复制并重命名该文件。我对如何正确调用 SaveImage 方法感到困惑。

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
    if (scanningResult != null) {
        isbn = scanningResult.getContents();
        SaveImage();
    }
    else{
        Toast toast = Toast.makeText(getApplicationContext(),
                "No scan data received!", Toast.LENGTH_SHORT);
        toast.show();
    }
}


private void SaveImage(Bitmap finalBitmap){
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/Movies/");
    String fname = "Image-"+ isbn +".jpg";
    File file = new File (myDir, fname);
    try {
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

【问题讨论】:

  • 这个问题可能不清楚。您想将该图像复制到哪里?
  • @TruongHieu 在同一个文件夹中。我知道我是菜鸟。我只是想复制同一个文件并重命名它。
  • isbn 是没有扩展名的位图文件名吧?

标签: java android bitmap fileoutputstream


【解决方案1】:

我只是想复制同一个文件并重命名它

感谢您更清楚地说明。您可以使用它从source 文件复制到destination 文件。

public void copy(File src, File dst) throws IOException {
    InputStream in = new FileInputStream(src);
    OutputStream out = new FileOutputStream(dst);

    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

【讨论】:

  • 好的。所以在参数“文件src,文件dst”中。我如何制作这些变量。例如。 src = "/movies/file.png" dst = "/movies/file2.png"。我正在努力学习,但对我来说有很多线索和错误。
  • 没有。您的源文件和目标文件是 Environment.getExternalStorageDirectory().toString() + /Movies/file.pngEnvironment.getExternalStorageDirectory().toString() + /Movies/file2.png
  • 感谢您的宝贵时间。我确定您给出了正确的答案,但我只需要另一个答案。再次感谢您的宝贵时间。
  • @user2631279 没关系,这很好,因为你解决了你的问题。祝你好运!
【解决方案2】:

所以您的问题是,如何正确调用您的 SaveImage(Bitmap finalBitmap) 方法,对吗?当您的SaveImage 方法获取位图作为参数时,您需要将位图作为参数发送给它。

您可以使用BitmapFactory 从您的文件中创建一个位图对象,并将此位图对象发送到您的SaveImage 方法:

String root = Environment.getExternalStorageDirectory().toString();
Bitmap bMap = BitmapFactory.decodeFile(root + "/Movies/myimage.png");
SaveImage(bMap);

【讨论】:

  • 谢谢。很高兴能回答我的愚蠢问题而不会受到羞辱。
  • 但是你羞辱了 Rahul Sharma 和 TruongHieu。他们为您的问题提供了唯一正确的答案。将 .png 文件转换为位图然后以不同的名称另存为 jpg 是完全错误的。如果你想复制一个文件——就像你问的那样——你应该复制这个文件。不使用位图。并将 .png 转换为 .jpg 你也没有提到,但 accecpt 是正确的答案。奇怪。
【解决方案3】:

重命名文件:

File source =new File("abc.png");
File destination =new File("abcdef.png");
source.renameTo(destination);

复制文件:

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

Path source=Paths.get("abc.png");
Path destination=Paths.get("abcdef.png");
Files.copy(source, destination);

【讨论】:

    猜你喜欢
    • 2020-03-05
    • 2015-07-13
    • 1970-01-01
    • 2021-05-06
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 2014-09-11
    相关资源
    最近更新 更多