【问题标题】:Save photo to memory card instead of local将照片保存到存储卡而不是本地
【发布时间】:2012-09-04 22:08:04
【问题描述】:

我有以下代码拍照并保存。但我想将最终图像保存到 SD 卡。

public class TakePhoto extends Activity {

    ImageView iv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_photo);


        iv = (ImageView) findViewById(R.id.imageView1);

        Button b = (Button) findViewById(R.id.button1);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent, 0);

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        Bitmap bm = (Bitmap) data.getExtras().get("data");
        writeBitmapToMemory("image.png", bm);
        iv.setImageBitmap(bm);

    }

    public void writeBitmapToMemory(String filename, Bitmap bitmap) {
        FileOutputStream fos;

        try {
            fos = this.openFileOutput(filename, Context.MODE_PRIVATE);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.close();

        } 
        catch (FileNotFoundException e) {
            e.printStackTrace();


        } 
        catch (IOException e) {
            e.printStackTrace();


        }

    }

似乎有一点正在发挥作用。我正在尝试保存到 SD 卡,但出现错误:(pastebin.com/FHihS4Wv)

我在 onActivityResult 上更改了writeBitmapToMemory("/mnt/extSdCard/image.png", bm);—— 但它因上面的 pastebinned 错误而崩溃

【问题讨论】:

    标签: java android android-camera


    【解决方案1】:

    根据这个问题java.lang.IllegalArgumentException: contains a path separator 您得到它是因为您尝试使用String 对象而不是File 对象访问子目录

    尝试将File 对象传递给该特定图像文件的函数,而不是包含文件名的字符串。

    类似这样的:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
    
        Bitmap bm = (Bitmap) data.getExtras().get("data");
        File imageFile = new File("image.png");
        writeBitmapToMemory(imageFile, bm);
        iv.setImageBitmap(bm);
    
    }
    
    public void writeBitmapToMemory(Filefile, Bitmap bitmap) {
        FileOutputStream fos;
    
        try {
            // fos = this.openFileOutput(file, Context.MODE_PRIVATE);
            fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.close();
    
        } 
    ....
    

    编辑: 它现在应该可以工作了,但我没有测试它。

    【讨论】:

    • 谢谢。不过有一个问题,我现在在 openFileOutput(...) 上遇到错误,openFileOutput (如 tiag.me/pics/error.png 所示)
    • 感谢您的编辑和帮助。我很感激。它没有错误,但由于某种原因没有图像。我的文件设置为 'File imageFile = new File("/mnt/extSdCard/image.png");'
    • 您是否在清单中设置了必要的权限?此外,这可能与 dirs heirarachy 有关。检查这个答案,例如如何做到这一点stackoverflow.com/a/7844792/975959
    • 是的,我有,复制和粘贴错误是我自己的错。非常感谢您的帮助伙伴,真的很感激! - 汤姆
    • 如果您不介意,我还有一个问题(如果您愿意,我不介意将其作为一个新问题提出:)) - 当它保存图片时,它们的质量非常低,而且小
    猜你喜欢
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多