[英]Transfer Image picked from gallery to another activity
我正在嘗試將畫廊或相機選擇的m圖像發送到另一個活動,但是似乎什么都沒有發生,我已經將位圖轉換為字符串以通過意圖發送它,但似乎從未調用該意圖。
請幫助我,我在做什么錯?
這是我的onActivityResult()代碼
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Bitmap thumbnail = null;
if(requestCode==RESULT_LOAD_IMAGE){
if(resultCode==RESULT_OK){
Uri selectedImageUri = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImageUri,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
data.getExtras().get("data");
thumbnail = (BitmapFactory.decodeFile(picturePath));
//CONVERT BITMAP TO STRING
ByteArrayOutputStream baos=new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.PNG,100, baos);
byte [] b=baos.toByteArray();
String temp=Base64.encodeToString(b, Base64.DEFAULT);
//trying to start activity...
Intent intent = new Intent(Home.this, Upload.class);
intent.putExtra("picture", temp);
startActivity(intent);}
}
}
}
為何不將圖像轉換為字符串,而不使用文件的路徑,然后在其他活動中重新加載它呢? 我的應用程序Pic4Share遇到了同樣的問題,並且以這種方式解決了。
在從圖庫加載圖像的活動中:
String stringPath;
protected void onActivityResult(int requestCode, int resultCode,Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case SELECT_PHOTO:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
stringPath = cursor.getString(columnIndex); // ***Here is stored the path***
cursor.close();
BitmapFactory.Options opts = new BitmapFactory.Options ();
opts.inSampleSize = 4; // for 1/2 the image to be loaded
opts.inPurgeable = true;
image.setImageBitmap(decodeSampledBitmapFromFile(txtPath, 800, 800));
}
else{
//Toast.makeText(getApplicationContext(),"Error", Toast.LENGTH_SHORT).show();
}
}
}
}
然后,您可以使用意圖將字符串與路徑一起傳遞:
Intent intent = new Intent(Home.this, Upload.class);
Bundle b = new Bundle();
b.putString("picture", stringPath);
intent.putExtras(b);
startActivity(intent);
最后,在其他應用程序中,您將使用以下路徑再次加載圖片:
Bundle b = getIntent().getExtras();
String picturePath = b.getString("picture");
Bitmap scaledBitmap = decodeSampledBitmapFromFile(picturePath, 150, 150);
我不知道您是否需要在String對象中轉換位圖以尊重您活動中的某些邏輯。 如果不是這種情況,那么轉換位圖- >字符串成為無用的,因為Bitmap
實現Parcelable
,所以你可以通過意圖傳遞putExtra(String name, Parcelable value)
的方法。
您可以在此鏈接中找到一些代碼
例:
發送意向
Intent intent = new Intent(Home.this, Upload.class);
intent.putExtra("picture", thumbnail);
並從其他活動中檢索
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("picture");
希望能幫助到你
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.