在我的android应用程序中,当我点击按钮时,新的活动开始并从SD卡中检索图像信息并显示它。当我点击图像的缩略图之一时,我想将该图像发送到第一个活动。我如何使用startactivityforresult来完成这项工作。如何存储图像路径并将其发送回第一个活动
第一次行动-
public void importFile(View v){
Intent intent=new Intent(this,ImportFile.class);
startActivityForResult(intent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1)
{
String path=data.getDataString();
}
}
第二项活动-
int j=0;
File[] imagefile;
File f = new File("/sdcard");
File[] files = f.listFiles();
for(int i = 0; i < files.length; i++) {
File file1 = files[i];
imagefile[j]=file1; //here getting nullpointer exception
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int j;
// TODO Auto-generated method stub
for(j=1;j<idcount;j++){
if(ch[j].isChecked())
{
System.out.println("PPPPPPPPPPPPPPPPPP"+j);
i=new Intent();
i.putExtra("file","mmm");//here how can i send image path
setResult(RESULT_OK, i);
finish();
}
}
}
由于您已经在临时文件中设置了文件名,请尝试从临时文件中检索它。。
字符串路径=data.getStringExtra("file");
假设您在Import.java活动中从SD卡中检索图像,如下所示:
File file = new File(getExternalFilesDir(null), "MyFile.jpg");
因此,一旦您的图像位于File对象中,您只需要将其路径放在Intent上,该Intent将被用作结果数据,并将其发送回"调用者"活动。在你所谓的活动中,你应该这样做:
Intent resultData = new Intent();
resultData.putExtra("imagePath", file.getAbsolutePath());
setResult(RESULT_OK,returnIntent);
finish();
您在ActivityResult:上的方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==RESULT_OK)
{
String path = data.getStringExtra("imagePath");
}
}
就是这样!
试试这个答案:
使用此功能,您可以从Gallery获取图像,并将其放在uri的imageview或arraylist上。这里的filepath是您从sd卡中选择的照片图像路径。您可以直接将其添加到Uri的数组列表中。也可以使用image.setImageBitmap()将图像设置为位图;
Intent intent = new Intent(Intent.ACTION_PICK , android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
YourActivity.this.startActivityForResult(intent,SELECT_PICTURE);
onActivityResult方法:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String filePath;
Log.i(TAG, "onActivityResult,requestCode" + requestCode);
if (requestCode == SELECT_PICTURE) {
try {
System.out.println("I ma here1");
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
filePath = cursor.getString(columnIndex);
Log.i(TAG, "======gallery image path=======:" + filePath);
// ======gallery image path=======:/mnt/sdcard/wallpaper4.jpg
// imageViewPhoto.setImageURI(selectedImage);
cursor.close();
Bitmap yourSelectedImage = null;
yourSelectedImage = BitmapFactory.decodeFile(filePath);
image_sublable.setImageBitmap(yourSelectedImage);
ByteArrayOutputStream os = new ByteArrayOutputStream();
yourSelectedImage.compress(CompressFormat.JPEG, 100, os);
image = os.toByteArray();
imagefilePath = filePath;
Log.i(TAG, "image in method=>" + image);
} catch (Exception e) {
e.printStackTrace();
}
}
您可以直接在字符串中添加文件路径,并将其存储到字符串数组中。试着用这个来获得一些想法。
int j=0;
String[] imagefile = new String[]{};
String fileString = filePath; // copy file path into fileString
String[] files = f.listofString();
for(int i = 0; i < files.length; i++) {
String fileString = files[i];
imagefile[j]=fileString; //here getting nullpointer exception
}
希望它能帮助你。