我从今天早上开始坚持这个:文件夹中没有创建图像 你能帮助我并告诉我它为什么不起作用吗?
获取图像是否正确?
Bundle extras = data.getExtras();
Bitmap imageToSave = extras.getParcelable("data");
这是我的完整代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
final File path =
Environment.getExternalStoragePublicDirectory
(
// Environment.DIRECTORY_PICTURES + "/ss/"
//Environment.DIRECTORY_DCIM
Environment.DIRECTORY_DCIM + "/MyFolderName/"
);
// Make sure the Pictures directory exists.
if(!path.exists())
{
path.mkdirs();
}
// Bitmap imageToSave = (Bitmap) data.getExtras().get("data");
// Bitmap imageToSave = (Bitmap) data.getData();
Bundle extras = data.getExtras();
Bitmap imageToSave = extras.getParcelable("data");
final File file = new File(path, "file" + ".jpg");
try {
FileOutputStream fos = new FileOutputStream(path);
final BufferedOutputStream bos = new BufferedOutputStream(fos, 8192);
FileOutputStream out = new FileOutputStream(path);
//fos = new FileOutputStream(path);
imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, fos);
// imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Uri selectedImage = data.getData();
Intent i = new Intent(this,
AddImage.class);
i.putExtra("imagePath", selectedImage.toString());
startActivity(i);
}
}}
答案 0 :(得分:2)
这应该有所帮助:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
{
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cur =
getContentResolver().query
(
selectedImage, filePathColumn, null, null, null
);
cur.moveToFirst();
String picturePath = cur.getString(cur.getColumnIndex(filePathColumn[0]));
cur.close();
// String picturePath contains the path of the selected Image
// Now you can copy it, send it to a server, load it into an ImageView...
}
}
答案 1 :(得分:1)
Data.getExtra()
退回Uri
;您应将其转换为filepath
。