从android中的sdcard检索图像

时间:2013-02-15 11:46:01

标签: android image file android-intent start-activity

在我的Android应用程序中,当我点击按钮时,新的活动开始并从sdcard中检索图像信息n显示它。当我点击图像的缩略图之一时,我想将该图像发送到第1个活动。我可以使用startactivityforresult如何存储图像路径并将其发送回第一个activity.i想要数组存储uri关闭所有图像,因为我动态列出它们

在第一次活动中 -

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

3 个答案:

答案 0 :(得分:0)

由于您已在附加内容中设置了文件名,请尝试从附加内容中检索它。

String path = data.getStringExtra(“file”);

答案 1 :(得分:0)

让你从Import.java Acitivty中的sdcard中检索图像:

File file = new File(getExternalFilesDir(null), "MyFile.jpg");

因此,一旦将图像放在File对象中,您只需将其路径放在将用作结果数据的Intent上,该结果将被发送回“调用者”活动。在你的“被叫”活动的某些方面,你应该这样做:

 Intent resultData = new Intent();
 resultData.putExtra("imagePath", file.getAbsolutePath());
 setResult(RESULT_OK,returnIntent);     
 finish();

你的方法onActivityResult:

@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");           

   }

}

就是这样!

答案 2 :(得分:0)

试试这个答案:

通过使用此功能,您可以从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();
            }
        }

您可以直接在字符串中添加文件路径并将其存储到String数组中。尝试使用它并获得一些想法。

 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
                 }

希望它会对你有所帮助。