将图像保存到SD卡文件夹

时间:2013-10-01 08:39:14

标签: android android-intent

我正在尝试在SD卡上创建一个文件夹,并将所有图像保存在那里,而不是内存。

public class CameraActivity extends Activity {

    private static final int CAMERA_PIC_REQUEST = 1111;
    private ImageView mImage;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImage = (ImageView) findViewById(R.id.result);
        //1
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
      File image = new File(Environment.getExternalStorageDirectory()+File.separator +"mypics");
    Uri fileUri = Uri.fromFile(image);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

        startActivityForResult(intent, CAMERA_PIC_REQUEST);
    }


    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_PIC_REQUEST) {
            //2
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
            EditText etext = (EditText) findViewById(R.id.editTextLocation);
            Toast.makeText(getBaseContext(), data.getExtras().get("data").toString(), Toast.LENGTH_LONG).show();
            mImage.setImageBitmap(thumbnail);
            etext.setText(data.getExtras().get("data").toString());
            //3
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            //4
            String state=Environment.getExternalStorageState();
            Toast.makeText(getBaseContext(), state, Toast.LENGTH_LONG).show();
            File file = new File(Environment.getExternalStorageDirectory()+File.separator +"mypics");
            if(!file.exists())
                if(file.mkdirs())
                    Log.v("Mobitracker","success");

             if (Environment.MEDIA_MOUNTED.equals(state))
                 Toast.makeText(getBaseContext(), "Yes its read only", Toast.LENGTH_LONG).show();
             if ( Environment.isExternalStorageRemovable ())
                 Toast.makeText(getBaseContext(), "Yes its internal card", Toast.LENGTH_LONG).show();

             //Create New file and name it Image2.PNG
             File file1 = new File(file, "Image2.PNG");
            try {
                file1.createNewFile();
                FileOutputStream fo = new FileOutputStream(file1);
                //5
                fo.write(bytes.toByteArray());
                fo.flush();
                fo.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        else
            Toast.makeText(getBaseContext(), "Please take snap again", Toast.LENGTH_LONG).show();
    }
}

这是我的代码,我尝试了很多东西但是仍然不可能在内部存储DCIM文件夹中保存为什么?

输出为

装 是的只读

我没有连接到系统,但仍然是同样的问题我甚至尝试重新启动手机,但所有但仍然相同。 我正在使用sony xperia tipo mobile 4.0.4 os。

请帮助我,让我感到沮丧,我尝试了所有的线程。

你能帮我吗

我的权限是

<uses-permission android:name="android.permission.CAMERA" />
 <uses-feature android:name="android.hardware.camera" />
 <uses-feature android:name="android.hardware.camera.autofocus" />
 <uses-permission android:name="android.permission.RECORD_AUDIO" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />  

我也可以在图像视图中看到位图图像   Toast.makeText(getBaseContext(),data.getExtras()。get(“data”)。toString(),Toast.LENGTH_LONG)。show();

正在返回一些android.graphics@422020的一些东西

2 个答案:

答案 0 :(得分:0)

您应该使用outputstream来编写内容。请参阅here寻求帮助。

或者做一些事情(参见here完整的主题):

Button.OnClickListener buttonSaveOnClickListener
 = new Button.OnClickListener(){

  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   OutputStream outStream = null;
   File file = new File(extStorageDirectory, "er.PNG");
   try {
    outStream = new FileOutputStream(file);
    bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
    outStream.flush();
    outStream.close();

    Toast.makeText(AndroidWebImage.this, "Saved", Toast.LENGTH_LONG).show();

   } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    Toast.makeText(AndroidWebImage.this, e.toString(), Toast.LENGTH_LONG).show();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    Toast.makeText(AndroidWebImage.this, e.toString(), Toast.LENGTH_LONG).show();
   }

  }

};

答案 1 :(得分:0)

仔细阅读this Android培训帖子以了解情况。正如帖子所说,

 The Android Camera application saves a full-size photo if you give it a file to save into.

因此,如果您确实传递了文件的Uri,它将仅存储在DCIM中,您应该使用此Uri访问该图像。

Check out similar thread