Java 如何保存图片并在PC上访问?
我正在关注Android应用程序的开发。此时,我能够拍摄照片,保存它,显示路径,并将位图显示到ImageView中 下面是logcat的一个示例,当我询问刚刚拍摄的照片的绝对路径时:Java 如何保存图片并在PC上访问?,java,android,android-camera-intent,android-external-storage,Java,Android,Android Camera Intent,Android External Storage,我正在关注Android应用程序的开发。此时,我能够拍摄照片,保存它,显示路径,并将位图显示到ImageView中 下面是logcat的一个示例,当我询问刚刚拍摄的照片的绝对路径时: D/PATH::/storage/simulated/0/Pictures/JPEG\u 20160210\u 140144\u 21764256.jpg 现在,我想通过USB将其传输到PC上。当我进入设备存储器时,我可以看到我前面在代码中使用变量Environment.DIRECTORY\u PICTURES调用
D/PATH::/storage/simulated/0/Pictures/JPEG\u 20160210\u 140144\u 21764256.jpg
现在,我想通过USB将其传输到PC上。当我进入设备存储器时,我可以看到我前面在代码中使用变量Environment.DIRECTORY\u PICTURES
调用的公用文件夹Picture
。但是,此文件夹中没有任何内容
我无法在设备中插入SD卡进行测试。另外,我不想把图片放入缓存目录,以防止被删除
以下是我在清单中的权限:
<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" />
这是创建文件的方法
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;
}
我想我误解了关于外部存储的一些内容有人能解释一下为什么我不能保存图片并在电脑上访问吗?谢谢
--编辑--
在阅读了下面的答案后,我试图在activityresult中获取文件,并用Java IO保存它。不幸的是,当我使用资源管理器查看时,图片文件夹中没有文件
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());
您当前正在将该文件保存为临时文件,因此在应用程序生命周期结束后,它将不会在磁盘上持久存在。使用类似于:
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());
为了解决我的问题,我必须将文件写入应用程序的数据文件夹,并使用MediaScannerConnection
。我已经放置了一个.txt文件进行测试,但在它工作后,您可以放置任何其他文件
我将为那些有类似问题的人分享解决方案:
try
{
// Creates a trace file in the primary external storage space of the
// current application.
// If the file does not exists, it is created.
File traceFile = new File(((Context)this).getExternalFilesDir(null), "TraceFile.txt");
if (!traceFile.exists())
traceFile.createNewFile();
// Adds a line to the trace file
BufferedWriter writer = new BufferedWriter(new FileWriter(traceFile, true /*append*/));
writer.write("This is a test trace file.");
writer.close();
// Refresh the data so it can seen when the device is plugged in a
// computer. You may have to unplug and replug the device to see the
// latest changes. This is not necessary if the user should not modify
// the files.
MediaScannerConnection.scanFile((Context)(this),
new String[] { traceFile.toString() },
null,
null);
}
catch (IOException e)
{
Log.d("FileTest", "Unable to write to the TraceFile.txt file.");
}
为了解决我的问题,我必须将文件写入应用程序的数据文件夹,并使用MediaScannerConnection
。我已经放置了一个.txt文件进行测试,但在它工作后,您可以放置任何其他文件
我将为那些有类似问题的人分享解决方案:
try
{
// Creates a trace file in the primary external storage space of the
// current application.
// If the file does not exists, it is created.
File traceFile = new File(((Context)this).getExternalFilesDir(null), "TraceFile.txt");
if (!traceFile.exists())
traceFile.createNewFile();
// Adds a line to the trace file
BufferedWriter writer = new BufferedWriter(new FileWriter(traceFile, true /*append*/));
writer.write("This is a test trace file.");
writer.close();
// Refresh the data so it can seen when the device is plugged in a
// computer. You may have to unplug and replug the device to see the
// latest changes. This is not necessary if the user should not modify
// the files.
MediaScannerConnection.scanFile((Context)(this),
new String[] { traceFile.toString() },
null,
null);
}
catch (IOException e)
{
Log.d("FileTest", "Unable to write to the TraceFile.txt file.");
}
运行应用程序时会发生什么情况?你有错误吗?没有错误。我运行主活动,然后调用MediaStore.ACTION\u IMAGE\u CAPTURE。我拍了一张照片,最后它回到了主要活动。当你运行你的应用程序时会发生什么?你有错误吗?没有错误。我运行主活动,然后调用MediaStore.ACTION\u IMAGE\u CAPTURE。我拍了一张照片,最后回到了主要活动。我按照你的建议添加了一些代码。我编辑了我的主要帖子。我按照你的建议添加了一些代码。我编辑了我的主要帖子。