将图像从ImageView保存到内部存储器 [英] Saving an image from ImageView into internal storage

查看:113
本文介绍了将图像从ImageView保存到内部存储器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了ImageView,可以看到相机的预览并将捕获的图像加载到ImageView中,并且我想将图像存储到内存中的目录中.我已经引用了很多帖子并尝试过,但是我在内存中找不到自己的图片.

I have created an ImageView and I can see the preview of the camera and load the captured image into the ImageView and I wanted to store the image into a directory in my internal memory. I have referred many posts and tried but I couldn't find my image in my internal memory.

这是我使用的代码:

package com.example.shravan.camera;

import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private final String TAG = "abc";

    static final int REQUEST_IMAGE_CAPTURE =1 ;
    ImageView iv;
    Uri imageUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button btn = (Button)findViewById(R.id.myB);
        iv = (ImageView) findViewById(R.id.myIV);
        //disable the button if the user has no camera
        if (!hasCamera()) {
            btn.setEnabled(false);
        }
    }

    public boolean hasCamera() {
        return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
    }

    //on click event handler that launches the camera
    public void launchCamera(View v) {
        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(i, REQUEST_IMAGE_CAPTURE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            imageUri=data.getData();
            iv.setImageURI(imageUri);;
        }
    }

    public void SaveFile(View v) {
        BitmapDrawable drawable = (BitmapDrawable) iv.getDrawable();
        Bitmap bitmap = drawable.getBitmap();

        print("Creating cw");
        ContextWrapper cw = new ContextWrapper(this.getApplicationContext());
        print("Creating dir");
        File directory = cw.getDir("ImagesDir", Context.MODE_PRIVATE);
        print("Created dir" + directory);
        File mypath = new File(directory,"myImagesDGS.jpg");
        print("path is" + mypath);

        FileOutputStream fos = null;
        try {
            print("creating fos");
            fos = new FileOutputStream(mypath);
            print("Compressing bitmap");
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fos.close();
                print("fos closed");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void print(String str){
        Log.d(TAG, str);
    }

}

我已经编写了许多日志消息进行调试,并且得到了一条在手机中找不到的路径.

I have made many log messages to debug and I got one path which I couldn't find in my phone.

这是logcat:

08-07 21:47:37.089 11030-11030/com.example.shravan.camera D/abc: 创建cw 08-07

08-07 21:47:37.089 11030-11030/com.example.shravan.camera D/abc: Creating cw 08-07

21:47:37.089 11030-11030/com.example.shravan.camera D/abc:创建目录08-07

21:47:37.089 11030-11030/com.example.shravan.camera D/abc: Creating dir 08-07

21:47:37.099 11030-11030/com.example.shravan.camera D/abc:已创建 dir/data/user/0/com.example.shravan.camera/app_ImagesDir 08-07

21:47:37.099 11030-11030/com.example.shravan.camera D/abc: Created dir/data/user/0/com.example.shravan.camera/app_ImagesDir 08-07

21:47:37.099 11030-11030/com.example.shravan.camera D/abc:路径 是/data/user/0/com.example.shravan.camera/app_ImagesDir/myImagesDGS.jpg

21:47:37.099 11030-11030/com.example.shravan.camera D/abc: path is/data/user/0/com.example.shravan.camera/app_ImagesDir/myImagesDGS.jpg

08-07 21:47:37.099 11030-11030/com.example.shravan.camera D/abc: 创建fos

08-07 21:47:37.099 11030-11030/com.example.shravan.camera D/abc: creating fos

08-07 21:47:37.099 11030-11030/com.example.shravan.camera D/abc:压缩位图

08-07 21:47:37.099 11030-11030/com.example.shravan.camera D/abc: Compressing bitmap

08-07 21:47:42.589 11030-11030/com.example.shravan.camera D/abc:fos已关闭

08-07 21:47:42.589 11030-11030/com.example.shravan.camera D/abc: fos closed

我需要检查什么并且应该更改吗?请帮忙!

Is there anything I need to check and I should change? Please help!

推荐答案

让它正常工作!

我使用以下代码在文件存储中创建目录,然后存储图像:

I used this code to create a directory in my file storage and then store the image:

FileOutputStream outStream = null;

// Write to SD Card
try {
    File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File(sdCard.getAbsolutePath() + "/camtest");
    dir.mkdirs();

    String fileName = String.format("%d.jpg", System.currentTimeMillis());
    File outFile = new File(dir, fileName);

    outStream = new FileOutputStream(outFile);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
    outStream.flush();
    outStream.close();

    Log.d(TAG, "onPictureTaken - wrote to " + outFile.getAbsolutePath());

    refreshGallery(outFile);
} catch (FileNotFoundException e) {
    print("FNF");
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} finally {

}

我的权限:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

最后,最重要的是:

转到设备设置">设备">应用程序">应用程序管理器">您的应用程序">权限">启用存储权限"!

Go to Device Settings>Device>Applications>Application Manager>"your app">Permissions>Enable Storage permission!

这篇关于将图像从ImageView保存到内部存储器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆