安卓更多笔记

安卓拍照问题

方式一: 拍照后立刻返回,图片质量低

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
    startActivityForResult(takePictureIntent, REQUEST_CAMERA_1); // 启动系统相机
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) { // 如果返回数据
        if (requestCode == REQUEST_CAMERA_1) { // 判断请求码是否为REQUEST_CAMERA,如果是代表是这个页面传过去的,需要进行获取
            Bundle bundle = data.getExtras(); // 从data中取出传递回来缩略图的信息,图片质量差,适合传递小图片
            bitmap = (Bitmap) bundle.get("data"); // 将data中的信息流解析为Bitmap类型
            imageview.setImageBitmap(bitmap);// 显示图片
        }
    }
}

方法二: 拍照后存储到手机sd卡中

有的系统有问题,添加这个代码

// android 7.0系统解决拍照的问题
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure();

图片文件转为bitmap,并且简单的压缩

图片加载时就压缩,原来的图片不会改变大小

// 获取外部sd卡的图片
File sdPath = Environment.getExternalStorageDirectory();
File file = new File(sdPath,filename);
//压缩
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize=100;//直接设置它的压缩率,表示1/2
bitmap = BitmapFactory.decodeFile(file.getPath(), options);

bitmap转为InputStream流

// bitmap转为inputStream流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
InputStream is = new ByteArrayInputStream(baos.toByteArray());

ps.setBinaryStream(2, is, is.available()); //插入到数据库中

其他输入输出流的转换:
https://blog.csdn.net/h7870181/article/details/8663760/

File与Uri的转换

File file = new File("test.txt");
Uri dst = Uri.fromFile(file);

安卓读写文件

写入到内部存储,位于/data/data/包名/files目录下

String str="Hello";
String filename = "text.txt";
try {
    FileOutputStream fos = openFileOutput(filename, MODE_PRIVATE);
    fos.write(str.getBytes());
    fos.flush();
    fos.close();
    Toast.makeText(MainActivity.this, "写入了文本:"+str, Toast.LENGTH_SHORT).show();

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

读取内部存储

String filename ="text.txt";
try {
    FileInputStream fis = openFileInput(filename);
    byte[] bytes = new byte[fis.available()];
    while (fis.read(bytes)!=-1){} //读取文件
    
    String str = new String(bytes);
    Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

写入到外部sd卡中

// 弹窗申请读写权限
if(verifyStoragePermissions(MainActivity.this)==false)
     return;

 String str="Hello";
 // 如果sd卡挂载了
 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
     File sdPath = Environment.getExternalStorageDirectory();
     File file = new File(sdPath,"filename.txt");
     //写入文本可以使用BufferedWriter,写入字节流可以用OutputStream
     try{
         OutputStream out = new FileOutputStream(file);
         out.write(str.getBytes());
         out.flush();
         out.close();
         Toast.makeText(MainActivity.this, "写入了文本:"+str, Toast.LENGTH_SHORT).show();
     } catch (FileNotFoundException e) {
         e.printStackTrace();
     } catch (IOException e) {
         e.printStackTrace();
     }
 }

AndroidManifest.xml中添加权限

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
        tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

//安卓7.0以上还需要动态申请读写权限
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
        "android.permission.READ_EXTERNAL_STORAGE",
        "android.permission.WRITE_EXTERNAL_STORAGE" };

public static boolean verifyStoragePermissions(Activity activity) {

    try {
        //检测是否有写的权限
        int permission = ActivityCompat.checkSelfPermission(activity,
                "android.permission.WRITE_EXTERNAL_STORAGE");
        if (permission != PackageManager.PERMISSION_GRANTED) { // 如果没有权限,就去申请权限,并返回false
            // 没有写的权限,去申请写的权限,会弹出对话框
            ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
            return false;
        }else{ //如果已经有权限了,直接返回true
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

读取sd卡外部存储

// 弹窗申请读写权限
if(verifyStoragePermissions(MainActivity.this)==false)
     return;
String str="hello";
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
    File sdPath = Environment.getExternalStorageDirectory();
    File file = new File(sdPath, "filename.txt");
    try {
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(str.getBytes());
        fos.flush();
        fos.close();
        Toast.makeText(MainActivity.this, "OK", Toast.LENGTH_SHORT).show();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值