复制现有的 png 文件并以编程方式重命名
Copy existing png file and rename programmatically
我在 SD 卡上的文件夹 "Movies" 中有一个 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();
}
}
我只是想复制同一个文件并重命名它
谢谢你说得更清楚了。您可以使用它从 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();
}
所以你的问题是,如何正确调用你的 SaveImage(Bitmap finalBitmap)
方法,对吗?当您的 SaveImage
方法获取位图作为参数时,您需要将位图作为参数发送给它。
您可以使用 BitmapFactory 从您的文件创建一个 Bitmap 对象并将此 Bitmap 对象发送到您的 SaveImage
方法:
String root = Environment.getExternalStorageDirectory().toString();
Bitmap bMap = BitmapFactory.decodeFile(root + "/Movies/myimage.png");
SaveImage(bMap);
重命名文件:
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);
我在 SD 卡上的文件夹 "Movies" 中有一个 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();
}
}
我只是想复制同一个文件并重命名它
谢谢你说得更清楚了。您可以使用它从 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();
}
所以你的问题是,如何正确调用你的 SaveImage(Bitmap finalBitmap)
方法,对吗?当您的 SaveImage
方法获取位图作为参数时,您需要将位图作为参数发送给它。
您可以使用 BitmapFactory 从您的文件创建一个 Bitmap 对象并将此 Bitmap 对象发送到您的 SaveImage
方法:
String root = Environment.getExternalStorageDirectory().toString();
Bitmap bMap = BitmapFactory.decodeFile(root + "/Movies/myimage.png");
SaveImage(bMap);
重命名文件:
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);