188

私がしたいのは、画像を電話の内部メモリ(SDカードではない)に保存することです。

どうすればいいですか?

カメラからアプリの画像ビューに直接画像を取得しましたが、すべて正常に機能しています。

今私が望むのは、この画像を Image View から Android デバイスの内部メモリに保存し、必要に応じてアクセスすることです。

誰でもこれを行う方法を教えてもらえますか?

android初心者なので詳しく手順を教えていただけると助かります。

4

7 に答える 7

367

以下のコードを使用して、画像を内部ディレクトリに保存します。

private String saveToInternalStorage(Bitmap bitmapImage){
        ContextWrapper cw = new ContextWrapper(getApplicationContext());
         // path to /data/data/yourapp/app_data/imageDir
        File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
        // Create imageDir
        File mypath=new File(directory,"profile.jpg");

        FileOutputStream fos = null;
        try {           
            fos = new FileOutputStream(mypath);
       // Use the compress method on the BitMap object to write image to the OutputStream
            bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
        } catch (Exception e) {
              e.printStackTrace();
        } finally {
            try {
              fos.close();
            } catch (IOException e) {
              e.printStackTrace();
            }
        } 
        return directory.getAbsolutePath();
    }

説明 :

1. 指定した名前でディレクトリが作成されます。Javadocs は、ディレクトリを作成する場所を正確に伝えるためのものです。

2.保存するイメージ名を指定する必要があります。

内部メモリからファイルを読み込みます。以下のコードを使用

private void loadImageFromStorage(String path)
{

    try {
        File f=new File(path, "profile.jpg");
        Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
            ImageView img=(ImageView)findViewById(R.id.imgPicker);
        img.setImageBitmap(b);
    } 
    catch (FileNotFoundException e) 
    {
        e.printStackTrace();
    }

}
于 2013-07-16T11:00:18.370 に答える
0

// 複数の画像を取得

 File folPath = new File(getIntent().getStringExtra("folder_path"));
 File[] imagep = folPath.listFiles();

 for (int i = 0; i < imagep.length ; i++) {
     imageModelList.add(new ImageModel(imagep[i].getAbsolutePath(), Uri.parse(imagep[i].getAbsolutePath())));
 }
 imagesAdapter.notifyDataSetChanged();
于 2020-07-18T13:27:14.250 に答える