我的项目有问题。我想将裁剪的图像传递给其他活动。我已经完成了我的裁剪方法,但我无法获得它的真实路径。我之前已经搜索了如何获得路径,但我找到的所有方法都没有用。它总是给我零路径。所以,如果你们可以帮助我,请回答我的问题.. 这是我的代码
public class MainActivity extends AppCompatActivity {
Button btn;
ImageView imgView;
private Uri mImageCaptureUri;
String realPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
imgView = (ImageView) findViewById(R.id.imageView);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
i.putExtra("crop","true");
i.putExtra("aspectX", 1);
i.putExtra("aspectY", 1);
i.putExtra("outputX", 200);
i.putExtra("outputY", 200);
i.putExtra("return-data", true);
startActivityForResult(i,2);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2 && resultCode == RESULT_OK && data != null){
Uri uri = data.getData();
String type = data.getType();
Log.i("TAG", "Pick completed: " + uri + " " + type);
if (uri != null)
{
String path = uri.toString();
if (path.toLowerCase().startsWith("file://"))
{
// Selected file/directory path is below
path = (new File(URI.create(path))).getAbsolutePath();
Log.e("ini", path);
}
}
Bundle extras = data.getExtras();
Bitmap img = extras.getParcelable("data");
imgView.setImageBitmap(img);
}
}
如果我得到了真正的路径,我可以使用intent传递路径并将其应用于下一个imageview。我需要的只是路径。感谢
答案 0 :(得分:0)
数据extraBundle中存在源URI。
Bundle extras = data.getExtras();
Bitmap img = extras.getParcelable("data");
imgView.setImageBitmap(img);
Uri selectedImageUri = Uri.parse(extras.get("src_uri").toString());
String realPath = getRealPathFromURI(selectedImageUri);
public String getRealPathFromURI(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}