gpt4 book ai didi

java - 如何保存图片并在电脑上访问它?

转载 作者:行者123 更新时间:2023-12-02 03:57:13 26 4
gpt4 key购买 nike

我正在关注 Google Camera Tutorial对于 Android 应用程序。此时,我可以拍照、保存、显示路径并将位图显示到 ImageView 中。

以下是当我请求刚拍摄的图片的绝对路径时的 logcat 示例:

D/PATH::/storage/emulated/0/Pictures/JPEG_20160210_140144_217642556.jpg

现在,我想通过 USB 将其传输到 PC 上。当我浏览设备存储时,我可以看到我之前在代码中使用变量 Environment.DIRECTORY_PICTURES 调用的公共(public)文件夹 Picture。但是,该文件夹中没有任何内容。

Screenshot of my device's folders

无法在我的设备中插入 SD 卡进行测试。另外,我不想将图片放入缓存目录以防止被删除。

这是我在 list 中的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

当用户单击相机按钮时:

dispatchTakePictureIntent();
[...]
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}

这是创建文件的方法

private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);

// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
Log.d("PATH:", image.getAbsolutePath());
return image;
}

我想我误解了关于外部存储的一些内容。 有人可以解释一下为什么我无法保存图片并在电脑上访问它吗?谢谢!

--编辑--

阅读下面的答案后,我尝试在 OnActivityResult 中获取文件并使用 Java IO 保存它。不幸的是,当我用资源管理器查看时,Pictures 文件夹中没有文件。

if (requestCode == REQUEST_TAKE_PHOTO) {
Log.d("AFTER", absolutePath);

// Bitmap bitmap = BitmapFactory.decodeFile(absolutePath);
// imageTest.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 2100, 3100, false));

moveFile(absolutePath, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString());
}

private void moveFile(String inputFile, String outputPath) {

InputStream in = null;
OutputStream out = null;
try {

//create output directory if it doesn't exist
File dir = new File (outputPath);
if (!dir.exists())
{
dir.mkdirs();
}


in = new FileInputStream(inputFile);
out = new FileOutputStream(outputPath + imageFileName + ".jpg");

byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;

// write the output file
out.flush();
out.close();
out = null;

// delete the original file
new File(inputFile).delete();


}

最佳答案

您当前正在将该文件保存为临时文件,因此在应用程序生命周期结束后它不会保留在磁盘上。使用类似的东西:

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File f = new File(Environment.getExternalStorageDirectory() + [filename])

然后创建一个FileOutputStream来写入它。

FileOutStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());

关于java - 如何保存图片并在电脑上访问它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35324550/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com