首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android如何保存文件?

Android如何保存文件?
EN

Stack Overflow用户
提问于 2017-04-12 09:58:18
回答 3查看 141关注 0票数 0

我需要把一些文件保存到我的Android手机里。所以我用了这样的方法:

代码语言:javascript
复制
FileOutputStream os = null;
 try{
     os = new FileOutputStream("/root/sdcard/DCIM/1.jpg");
     os.write(bytes);
     os.close();
 }catch(FileNotFoundException e){}

当我这么做的时候,它会说

代码语言:javascript
复制
java.io.FileNotFoundException: /root/sdcard/DCIM/1.jpg (Permission denied)

顺便说一句,我已经在AndroidManifest.xml中请求使用以下内容的许可:

代码语言:javascript
复制
<user-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

我也试过

代码语言:javascript
复制
getFilesDir().getAbsolutePath();

它实际上指的是

代码语言:javascript
复制
/data/user/0/come.package.xxx/files

我不知道这条路在哪里,因为我在手机上找不到它。当我使用时,我看到路径是/root/ sdcard /.,但是我的手机中甚至没有sdcard,我已经使用iPhone很多年了,所以我现在不知道Android文件系统是如何工作的。

这让我很困惑,有人能给我解释一下Android文件系统是如何工作的吗?谢谢大家!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-04-12 10:13:44

如果您正在使用Android6.0Marsh或更高版本的android,您需要给予代码下面的access.try运行时权限

代码语言:javascript
复制
  if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            camera.setEnabled(false);
           ActivityCompat.requestPermissions(getActivity(), new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
       }
 @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (requestCode == 0) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
                    && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                //permission will get success here
                //do what you want
            }
            else {
                //Permission not granted
                Toast.makeText(getActivity(),"You need to grant camera permission to use camera",Toast.LENGTH_LONG).show();
            }
        }
    }
票数 0
EN

Stack Overflow用户

发布于 2017-04-12 10:05:08

要在Android上保存图像:

代码语言:javascript
复制
private String saveToInternalStorage(String name, Bitmap bitmapImage){
        ContextWrapper cw = new ContextWrapper(context);
        // path to /data/data/yourapp/app_data/imageDir
        File directory = cw.getDir(IMAGE_TAG, Context.MODE_PRIVATE);
        // Create imageDir
        File mypath = new File(directory, name + ".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 (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return directory.getAbsolutePath();
    }

要加载图像:

代码语言:javascript
复制
public void loadImage(ImageView imageView) {
        // get path
        ContextWrapper cw = new ContextWrapper(context);
        File directory = cw.getDir(IMAGE_TAG, Context.MODE_PRIVATE);
        String path = directory.getAbsolutePath();

        // load image
        try {
            File f = new File(path, name + ".jpg");
            Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
            imageView.setImageBitmap(b);
            imageView.setVisibility(View.VISIBLE);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Log.d("Image", "Image file not found.");    
        }
}
票数 0
EN

Stack Overflow用户

发布于 2017-04-12 10:23:53

如果您正在使用棉花糖或最新版本的android,因此您需要在运行时提供权限,那么在您的“但是”单击时,您需要调用代码将文件保存到sd卡中。

比像这样做之后,

代码语言:javascript
复制
public void onClick(View v) {
    // write on SD card file data in the text box
    try {
        File myFile = new File("/sdcard/mysdfile.txt");
        myFile.createNewFile();
        FileOutputStream fOut = new FileOutputStream(myFile);
        OutputStreamWriter myOutWriter = 
                                new OutputStreamWriter(fOut);
        myOutWriter.append(txtData.getText());
        myOutWriter.close();
        fOut.close();
        Toast.makeText(getBaseContext(),
                "Done writing SD 'mysdfile.txt'",
                Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        Toast.makeText(getBaseContext(), e.getMessage(),
                Toast.LENGTH_SHORT).show();
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43366287

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档