如何将图像路径存储到array.as我正在动态检索图像。列出它们。在点击图像之后我想将它发送到上一个活动。 我该如何存储图像路径?请帮帮我
below is code-
第1次活动 - 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();
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();
}
}
}
答案 0 :(得分:0)
使用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字符串到数组,你可以使用该数组。
或强>
您可以在活动中创建静态数组,并可以在活动中静态使用它。
答案 1 :(得分:0)
尝试这样:
您的被叫活动:
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());
}
}
答案 2 :(得分:0)
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);
}