`getExternalStorageDirectory()`的问题 - Android电子问题、电子、getExternalStorageDirectory、Android

由网友(你烦我厌)分享简介:我写一个Android应用程序,需要一些图片,并想将它们全部保存有关我的应用程序的唯一目录。此目录应当从标准库,以这样的方式,用户可以稍后(当应用程序不一定运行)检查拍摄的图像可访问的我的问题是,每一个不同的手机供应商,以及不同的Andr​​oid版本,有gallery.As不同路径的例子: Environment....

我写一个Android应用程序,需要一些图片,并想将它们全部保存有关我的应用程序的唯一目录。

此目录应当从标准库,以这样的方式,用户可以稍后(当应用程序不一定运行)检查拍摄的图像可访问的

我的问题是,每一个不同的手机供应商,以及不同的Andr​​oid版本,有gallery.As不同路径的例子:

  Environment.getExternalStorageDirectory()+ Environment.DIRECTORY_PICTURES +/ MyFolder文件 

三星GALAXY Nexus 运行的Andr​​oid 4.1.1 ,并在华硕将工作变压器垫运行的Andr​​oid 4.0.3 ,但不能在的HTC Desire 运行的Andr​​oid 2.3.5

Android Environment.getExternalStorageDirectory问题

这会使我的应用程序试图挽救指定的路径内一个新的目录时崩溃,如下所述:

 布尔成功= FALSE;MyFolder中=新的文件(Environment.getExternalStorageDirectory()+ Environment.DIRECTORY_PICTURES +/ MyFolder文件);如果(myFolder.exists()){//没做什么}其他{    成功= dvaFolder.mkdir();如果(成功){    //做成功的东西    / *     *创建文件夹     * /}其他{    //做一些失败其他    / *     *文件夹创建失败     * /    抛出新的RuntimeException(文件错误书面新建文件夹);}} 

  

我怎么能写的目录,将访问在画廊的所有  不同的厂商和Android版本?

注意:

logcat的没有多少有用的,因为它只是返回运行时异常。

解决方案

下面是工作code为拍照并保存到外部存储,并保存到files目录下的设备的另一种方法。在我来说,我只需要一个图像(something.png),将用户采取新的照片后始终刷新。

时的动作按钮用户点击摄像机:

  ImageView的productImageView =(ImageView的)findViewById(R.id.imageView1);静态INT RESULT_TAKE_PICTURE = 1;串selectedImagePath;位图位图;字符串imageName =something.png;公共无效cameraImageButton_onClick(查看视图){    意图cameraIntent =新意图(            android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);    字符串路径= Environment.getExternalStorageDirectory()            +文件分割符+ Environment.DIRECTORY_PICTURES;    文件DIR =新的文件(路径);    如果(!dir.exists()){        dir.mkdir();    }    DIR =新的文件(路径,imageName);    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(DIR));    startActivityForResult(cameraIntent,RESULT_TAKE_PICTURE);} 

这是对活动的结果执行操作:

 保护无效的onActivityResult(INT申请code,INT结果code,意图数据){    super.onActivityResult(要求code,结果code,数据);    如果(要求code == RESULT_TAKE_PICTURE&放大器;&安培;结果code == RESULT_OK){        selectedImagePath = Environment.getExternalStorageDirectory()                +文件分割符+ Environment.DIRECTORY_PICTURES                +文件分割符+ imageName;        BitmapFactory.Options选项=新BitmapFactory.Options();        options.inJustDe codeBounds = TRUE;        BitmapFactory.de codeFILE(selectedImagePath,期权);        options.inSampleSize = Calculator.calculateInSampleSize(选项,                254,254);        options.inJustDe codeBounds = FALSE;        位= BitmapFactory.de codeFILE(selectedImagePath,期权);        productImageView.setImageBitmap(位图);        saveImage(位图,imageName);    }} 

在保存图像设备红利方式的文件的目录,如果有人需要:

 私人无效saveImage(位图位图,字符串名称){    字符串路径= getApplicationContext()。getFilesDir()。的toString()            +文件分割符;    文件DIR =新的文件(路径);    如果(!dir.exists()){        dir.mkdir();    }    路径=路径+名称;    DIR =新的文件(路径);    尝试{        dir.createNewFile();        FOS的FileOutputStream =新的FileOutputStream(DIR);        bitmap.com preSS(Bitmap.Com pressFormat.PNG,100,FOS);        fos.close();        fos.flush();    }赶上(IOException异常五){        e.printStackTrace();    }} 

I'm writing an android application that takes some pictures and would like to save them all in a unique directory associated to my application.

This directory should be accessible from the standard Gallery, in such a way that the user can later (when application is not necessarily running) check the pictures that were taken.

My problem is that every different phone vendor, with a different android version, has different paths for gallery.As an example:

Environment.getExternalStorageDirectory() + Environment.DIRECTORY_PICTURES +"/myFolder"

will work on Samsung Galaxy Nexus running android 4.1.1, and on Asus Transformer Pad running android 4.0.3, but not on HTC Desire running android 2.3.5.

This will cause my application to crash when trying to save a new directory within the specified path, as stated below:

boolean success = false;
myFolder = new File( Environment.getExternalStorageDirectory() + Environment.DIRECTORY_PICTURES + "/myFolder" );

if( myFolder.exists() ){
//do nothing        
}else{

    success = dvaFolder.mkdir();

if( success ){
    // Do something on success
    /*
     * folder has been created
     */

} else {
    // Do something else on failure 
    /*
     * folder creation failed
     */
    throw new RuntimeException("File Error in writing new folder");
}
}

How can I write a directory that will be accessible in gallery for all different vendors and android versions?

NOTE:

Logcat isn't much useful, cause it just returns the run time Exception.

解决方案

Here is working code for taking picture and saving to external storage and another method for saving to 'files' directory on device. In my case I needed only one image (something.png) which will be always refreshed after user takes new picture.

Action when user clicks on button for camera:

ImageView productImageView = (ImageView) findViewById(R.id.imageView1);
static int RESULT_TAKE_PICTURE = 1;
String selectedImagePath;
Bitmap bitmap;
String imageName = "something.png";

public void cameraImageButton_onClick(View view) {
    Intent cameraIntent = new Intent(
            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    String path = Environment.getExternalStorageDirectory()
            + File.separator + Environment.DIRECTORY_PICTURES;
    File dir = new File(path);
    if (!dir.exists()) {
        dir.mkdir();
    }
    dir = new File(path, imageName);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(dir));
    startActivityForResult(cameraIntent, RESULT_TAKE_PICTURE);
}

Action which is performed on activity result:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_TAKE_PICTURE && resultCode == RESULT_OK) {

        selectedImagePath = Environment.getExternalStorageDirectory()
                + File.separator + Environment.DIRECTORY_PICTURES
                + File.separator + imageName;

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(selectedImagePath, options);
        options.inSampleSize = Calculator.calculateInSampleSize(options,
                254, 254);
        options.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeFile(selectedImagePath, options);
        productImageView.setImageBitmap(bitmap);

        saveImage(bitmap, imageName);

    }

}

Bonus method for saving image to device in files directory if someone needs:

private void saveImage(Bitmap bitmap, String name) {
    String path = getApplicationContext().getFilesDir().toString()
            + File.separator;
    File dir = new File(path);

    if (!dir.exists()) {
        dir.mkdir();
    }

    path = path + name;
    dir = new File(path);

    try {
        dir.createNewFile();
        FileOutputStream fos = new FileOutputStream(dir);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();
        fos.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

阅读全文

相关推荐

最新文章