storing image path from sdcard in android

2019-08-29 09:11发布

问题:

how to store image path into array.as i'm retrieving images dynamically.n listing them.after clicking on one o the image i want to send it to previous activity. how can i store the image path??please help me

below is code-

1st activity- 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);
    }
}

2nd activity-

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();
            }       
        }
    }

回答1:

Using GSON library

http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/

you can create a json string of the image path array then store the json string to preference

http://www.javacodegeeks.com/2011/01/android-quick-preferences-tutorial.html

Storing value in preference can be accessible to all the activities. And from another activity convert json string to array and you can use that array.

OR

You can create a static array in an activity and can use it statically across the activities.



回答2:

Try like that:

Your Called Activity:

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();
}

Your Caller Activity:

@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());
  }

}



回答3:

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;

and then;

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);
        }