0

在我的应用程序中,我正在从 sdcard 中检索图像文件。n 将路径发送回先前的活动。但我无法在以前的活动中获得路径。请帮助我。下面是代码-

第一项活动-

public class Work extends Activity {
String imagepath;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

}

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)
    {
        //here i wanto get image path.
    }
}

}

第二个活动——

   public class ImportFile extends Activity implements OnCheckedChangeListener{

 TableLayout tl;
 Intent i;
 ImageView im;
 int idcount;
 Uri uri[];
 Uri[] imagefiles;
String filepath[];
String[] image=new String[100];
 TextView nameoffile,sizeoffile,line;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylist);
        File file = new File(Environment.getExternalStorageDirectory()
            + File.separator + "gallery" //folder name
        );
        if (!file.exists()) {
            file.mkdirs();
        }
          File f = new File("/sdcard/download");
            File[] files = f.listFiles();
        int j=0;
        for(int i = 0; i < files.length; i++,j++) {
            File file1 = files[i];
            //take the file name only
            long size = file1.length()/1024;
             String myfile = file1.getPath().substring(file1.getPath().lastIndexOf("/")+1,file1.getPath().length()).toLowerCase(); 
            System.out.println("file is : "+files[i]);
            if(myfile.endsWith(".jpeg")||myfile.endsWith(".png")|| myfile.endsWith(".gif") || myfile.endsWith(".jpg"))
            {
                System.out.println("file is : "+files[i]);
                image[j]=file1.getAbsoluteFile().toString();
            }               
        }
       }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        int j;
        imagesPaths = new ArrayList<String>();
        // 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("files",image[++j]);
                setResult(RESULT_OK, i);
                finish();
            }       
        }
    }
}  
4

1 回答 1

0
imagesPaths = new ArrayList<String>();
// TODO Auto-generated method stub
for(j=1;j<idcount;j++){
    if(ch[j].isChecked())
    {
        System.out.println("PPPPPPPPPPPPPPPPPP"+j);
        imagesPaths.add(image[++j]); 
    }   
}  
i=new Intent();
i.putExtra("files",imagesPaths);  
setResult(RESULT_OK, i);
finish();
}  



if(requestCode==1)
{
   ArrayList<String> imagesPaths = data.getStringArrayListExtra("files");

} 
于 2013-02-16T09:45:33.250 回答