如何将图像路径存储到数组中。因为我正在动态检索图像。n列出它们。点击其中一个图像后,我想将其发送到上一个活动。如何存储图像路径??请帮我
below is code-
第一项活动-public void importFile(视图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();
System.out.println(";;;;;;;;;;;;"+path);
}
}
第二项活动-
in oncreat-
int j=0;
File[] imagefile;
File f = new File("/sdcard");
File[] files = f.listFiles();
for(int i = 0; i < files.length; i++,j++) {
File file1 = files[i];
if(myfile.endsWith(".jpeg")){
image[j]=file1;//here i'm getting nullpointer exception
}
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int j;
// TODO Auto-generated method stub
for(j=1;j<idcount;j++){
if(ch[j].isChecked())
{
imagesPaths.add(image[++j].getAbsolutePath());
i=new Intent();
i.putExtra("files",imagesPaths);
setResult(RESULT_OK, i);
finish();
}
}
}
使用GSON库
http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
您可以创建映像路径数组的json字符串,然后将json字符串存储到首选
http://www.javacodegeeks.com/2011/01/android-quick-preferences-tutorial.html
所有活动都可以访问按偏好存储值。从另一个活动中,将json字符串转换为数组,就可以使用该数组。
或
您可以在活动中创建一个静态数组,并可以在活动之间静态使用它。
这样尝试:
您的被叫活动:
File f = new File("/sdcard");
File[] files = f.listFiles();
ArrayList<String> imagesPaths = new ArrayList<String>(); // Array to store the images paths
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
i=new Intent();
for(int j=0; j< ch.lenght; j++){
if(ch[j].isChecked())
{
imagePaths.add(f[j].getAbsolutePath());
}
}
i.putStringArrayListExtra("files", imagePaths);
setResult(RESULT_OK, i);
finish();
}
您的呼叫者活动:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1)
{
ArrayList<String> paths= data.getStringArrayListExtra("files");
System.out.println(paths.toString());
}
}
case 1:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
String path = getRealPathFromURI( selectedImage );
LogMsg.d("PAth "+path);
Bitmap b = getBitmap(path);
bitmapList.add(b);
adapter.notifyDataSetChanged();
}
break;
然后;
private String getRealPathFromURI(Uri contentUri) {
ContentResolver resolver = getContentResolver();
String[] proj = { MediaStore.Video.Media.DATA };
Cursor cursor = resolver.query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}