【问题标题】:getting image path while saving an image in android在android中保存图像时获取图像路径
【发布时间】:2012-01-07 17:17:47
【问题描述】:

我正在使用以下代码将图像保存到 SD 卡。 但是如何获取保存图像的图像路径,因为我想使用此路径在下一个活动中为 ImageView 设置图像。 我尝试使用onActivityResult(),但无法获得路径。 onActivitySesult()only 仅在我们想要打开浏览文件的意图时触发,但我想在不打开画廊或意图获取文件路径的情况下访问。 任何观点都会有所帮助。 期待你的答复。 谢谢。

PictureCallback myPictureCallback_JPG = new PictureCallback(){

            @Override
            public void onPictureTaken(byte[] arg0, Camera arg1) {
                // TODO Auto-generated method stub


                 FileOutputStream outStream = null;
                  try {
                    // Write to SD Card
                    outStream = new FileOutputStream(String.format("/sdcard/%d.jpg",System.currentTimeMillis())); 
                    outStream.write(arg0);
                    outStream.close();
                    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
                    Toast.makeText(Photo.this, "Image Saved to SD Card", Toast.LENGTH_SHORT).show();
                   System.out.println();
                  } catch (FileNotFoundException e) { 
                    e.printStackTrace();
                    //Log.e("Photo", "Image files get saved in SD Card only",e);
                  } catch (IOException e) {
                    e.printStackTrace();
                  }

                camera.startPreview();
            }};

【问题讨论】:

    标签: android


    【解决方案1】:

    试试这个方法:

    将 System.currentTimeMillis() 分配给 String 或 long 并将其传递给 File Outputstream。

        try {
            String stored_date=System.currentTimeMillis()+"";
            outStream = new FileOutputStream(String.format("/sdcard/%d.jpg",stored_date)); 
            outStream.write(arg0);
            outStream.close();
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
            Toast.makeText(Photo.this, "Image Saved to SD Card", Toast.LENGTH_SHORT).show();
           System.out.println();
        } catch (FileNotFoundException e) {}
        catch (IOException e) {}
    

    【讨论】:

    • 我确实尝试过,它成功了,将 stored_date 保存在共享首选项中,在另一个活动中,我从共享首选项中并通过 setImageUri 'imageAttachPhoto.setImageURI(Uri.parse("file:/ /" + Environment.getExternalStorageDirectory()+ "/"+文件名+".jpg"));'谢谢。
    • 关于如何知道 ImageView 是否设置了 Image 的任何帮助,如果 Image 已设置,那么我必须访问该 Image。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-12
    • 1970-01-01
    • 2012-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多