csh_34 2013-03-13 02:32 采纳率: 0%
浏览 8129
已采纳

在 Android 中保存图像获得图像的路径

我使用下面的代码把一个图像保存到SD card中,但是如何获得保存了的图像的路径,因为我想使用这个路径给下一个 activity 中的 ImageView 设置图像。
我试着使用onActivityResult(),但是不能获得路径。因为如果你想在浏览器文件夹中打开一个 intent,onActivitySesult()会被摧毁,但是我不想打开 gallery 或者 intent,就能访问获取文件路径。 请大家帮忙解决这个问题,谢谢。

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();
            }};
  • 写回答

2条回答 默认 最新

  • yongyong_21 2013-03-13 06:13
    关注

    指定 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) {}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法