【问题标题】:retrieving image from sdcard in android从android中的sdcard中检索图像
【发布时间】:2013-02-15 11:46:01
【问题描述】:

在我的 android 应用程序中,当我单击按钮时,新活动开始并从 sdcard 检索图像信息并显示它。当我单击图像的缩略图之一时,我想将该图像发送到第一个活动。我该如何使用startactivityforresult.how 存储图像路径并将其发送回第一个活动。我想要用于存储所有图像的 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();
            }       
        }
    }    

【问题讨论】:

  • 取决于您从 sdcard 获取图像的方式。

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


【解决方案1】:

既然您已经在附加文件中设置了文件名,请尝试从附加文件中检索它..

字符串路径= data.getStringExtra("file");

【讨论】:

  • 我可以存储路径吗?我使用了 File[] 图像文件;图像文件[id]=file1;其中,文件 f = new File("/sdcard");文件[] 文件 = f.listFiles();文件file1 = 文件[1];
  • 这应该有效: String imagePath = imagefiles[id].getAbsolutePath(); Intent resultData = new Intent(); resultData.putExtra("imagePath", imagePath); setResult(RESULT_OK,returnIntent);完成();
【解决方案2】:

假设您像这样从 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");           

   }

}

就是这样!

【讨论】:

【解决方案3】:

试试这个答案:

通过使用它,您可以从 Gallery 中获取图像并将其放在 uri 的 imageview 或 arraylist 中。这里的文件路径是您从 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();
            }
        }

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

 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
                 }

希望对你有所帮助。

【讨论】:

  • 如何在 Uri 数组中设置文件类型的路径。请参阅第二个活动中的代码
  • 检查我在上面编辑的第二个活动的答案。我认为你有空指针异常,因为你已经声明了数组的大小。
  • 如果你得到我的回答是有用的然后检查upvote,如果它解决了upvote下面的错误,那就做对了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多