我想制作一个附件功能,允许用户从图库中拍摄照片或选择图像,然后在附件列表视图中显示。
然后,当用户选择了他的图像时,结果是FileSchemeUri
还是ContentSchemeUri
。所以我尝试降低位图的大小并获取单击的文件路径以查看图像/在attachmnet列表视图中设置图标,方法是使用此函数获取outputFilePath:
private File uriToFile(Uri uri, File sdImageMainDirectory) throws IOException{
/*InputStream inputStream = getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);*/
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
OutputStream stream = new FileOutputStream(sdImageMainDirectory);
int bitmapWidth = bitmap.getWidth();
int bitmapHeight = bitmap.getHeight();
int maxSize = 640;
float bitmapRatio = (float) bitmapWidth / (float) bitmapHeight;
if(bitmapRatio > 0){
bitmapWidth = maxSize;
bitmapHeight = (int) (bitmapWidth / bitmapRatio);
}
else{
bitmapHeight = maxSize;
bitmapWidth = (int) (bitmapHeight * bitmapRatio);
}
bitmap.createScaledBitmap(bitmap, bitmapWidth, bitmapHeight, true);
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, stream);
File outputFile = new File(String.valueOf(stream));
stream.flush();
stream.close();
return outputFile;
}
我想要做的是将位图写入名为sdImageMainDirectory
的特定路径。它失败了,因为outputFile的值是这样的事情:java.IO.FileOutputStream@438571
。我看了看我的目录文件夹,那些图像变成了0kb图像(坏了)。我想问一下如何将位图写入那些目录?请帮忙。
调试:
答案 0 :(得分:0)
这是我选择的图像方法,请看一下:
private void imageChooserDialog() {
//region Create folder 'eLeaveAttachmentFolder'
// Define a file name "root", which is a path of the user's phone's image directory, and create a file called "eLeaveAttachmentFolder"
final File root = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + "eLeaveAttachmentFolder" + File.separator);
// In the file object "root" (path), if the folder eLeaveAttachmentFolder is not exist, create one.
root.mkdirs();
//endregion
//region Define where the result should save at
// Define a string name claimfilename, which is used to store an image's name while it's created/selected.
final String claimfilename = getUniqueImageFilenameForFilename();
// Define a file name sdImageMainDirectory which is exactly the image's path (e.g. emulated\0\Pictures\eLeaveAttachmentFolder\Screenshot_08122015_160952.jpg)
sdImageMainDirectory = new File(root, claimfilename);
//endregion
// Assign the path 'sdImageMainDirectory' to the outputFileUri(Image in uri format)
outputFileUri = Uri.fromFile(sdImageMainDirectory);
//Camera
// Create an arraylist to save types of intent (camera or gallery)
final List<Intent> cameraIntents = new ArrayList<>();
// Create the intent of camera
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// PackageManager manages uri installing and uninstalling
final PackageManager packageManager = getPackageManager();
// ?? What is ResolveInfo? packageManager.queryIntentActivities(captureIntent, 0) ??
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
// "res" is to get all objects from the array "listCam"
for (ResolveInfo res : listCam) {
// ?? ????
final String packageName = res.activityInfo.packageName;
// Create the intent for saving the captureIntent
final Intent intent = new Intent(captureIntent);
//
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
// Bundle the outputFileUri to onActivityResult
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
// Set the quality of the image
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
// Add the whole camera properties into one intent called cameraIntents
cameraIntents.add(intent);
}
// Filesystem.
final Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Source");
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
startActivityForResult(chooserIntent, SELECT_PICTURE); // Activity to pop up the dialog, bundle the selectedImage Data to show at the onActivityResult
}
//endregion
//region onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
//Camera
if (selectedImageUri == null) {
//selectedImageUri = outputFileUri;
try{uriToFile(outputFileUri, sdImageMainDirectory);}
catch(IOException e){}
}
else {
try{uriToFile(selectedImageUri, sdImageMainDirectory);}
catch(IOException e){}
}
}
try {imageOutputStream = new FileOutputStream(sdImageMainDirectory); }
catch (FileNotFoundException e) {}
claimattachmentfilename = getUniqueImageFilenameForShowing();
claimattachmentpathname = getUniqueImageFilenameForFilename();
/*try{claimattachmentbitmap = UriToBitmap(claimattachmenturi);}
catch (FileNotFoundException e){}
catch(IOException e){}*/
newAttachment = new AttachmentListResult();
// newAttachment.setBitmap(claimattachmentbitmap);
newAttachment.setDirectory(sdImageMainDirectory);
newAttachment.setFilename(claimattachmentfilename);
// newAttachment.setPathname(claimattachmentpathname);
// newAttachment.setByteArray(claimattachmentbytearray);
if (lastAdapter != null) {attachmentarray = lastAdapter.getAttachmentArray();}
attachmentarray.add(newAttachment);
listViewUpdate();
}
}
//endregion