-1

我试图在 sdcard 上创建一个文件夹并将所有图像保存在那里而不是内部存储器。

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 文件夹中,为什么?

我得到的输出为

已安装 是的,它是只读的

我没有连接到系统但仍然存在同样的问题我什至尝试重新启动手机,但仍然相同。我正在使用索尼 xperia tipo mobile 4.0.4 操作系统。

请帮助我沮丧我尝试了所有线程。

你们能帮帮我吗

我的权限是

<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 一些东西

4

2 回答 2

0

仔细阅读这篇Android 培训帖子以找出答案。正如帖子所说,

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

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

查看类似的线程

于 2013-10-01T08:56:36.613 回答
0

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

或者做类似的事情(见这里完整的线程):

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();
   }

  }

};
于 2013-10-01T08:48:44.487 回答