添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
玩篮球的火锅  ·  在Windows ...·  1 年前    · 
年轻有为的香蕉  ·  用Flink ...·  2 年前    · 
import android . content . ContentResolver ; import android . content . ContentUris ; import android . content . Context ; import android . database . Cursor ; import android . graphics . Bitmap ; import android . graphics . BitmapFactory ; import android . net . Uri ; import android . os . Environment ; import android . provider . MediaStore ; import android . text . TextUtils ; import android . util . Base64 ; import android . widget . ImageView ; import java . io . File ; import java . io . FileInputStream ; import java . io . FileOutputStream ; import java . io . IOException ; import java . io . InputStream ; import java . io . OutputStream ; * 图片相关的工具类 public class ImageUtils { * base64字符串转化成图片 public static String GenerateImage ( Context context , String imgStr ) { //对字节数组字符串进行Base64解码并生成图片 if ( imgStr == null ) { //图像数据为空 UtilsTools . MsgBox ( context , "图片不能为空" ) ; return "" ; try { //Base64解码 byte [ ] b = Base64Utils . decode ( imgStr ) ; for ( int i = 0 ; i < b . length ; ++ i ) { if ( b [ i ] < 0 ) { //调整异常数据 b [ i ] += 256 ; // 新生成的jpg图片 // 新图片的文件夹, 如果没有, 就创建 String dirPath = Environment . getExternalStoragePublicDirectory ( Environment . DIRECTORY_PICTURES ) . getAbsolutePath ( ) + "/zdgj/" ; File fileDir = new File ( dirPath ) ; if ( ! fileDir . exists ( ) ) { fileDir . mkdirs ( ) ; // 文件夹现在存在了, 可以在此文件夹下创建图片了 String imgFilePath = dirPath + System . currentTimeMillis ( ) + ".jpg" ; File file = new File ( imgFilePath ) ; if ( ! file . exists ( ) ) { file . createNewFile ( ) ; OutputStream out = new FileOutputStream ( imgFilePath ) ; out . write ( b ) ; out . flush ( ) ; out . close ( ) ; SharedPrefUtil . putString ( context , SharedPreConstant . FacePicPathKey , imgFilePath ) ; UtilsTools . MsgBox ( context , "图片已保存到本地" ) ; return imgFilePath ; } catch ( Exception e ) { UtilsTools . MsgBox ( context , e . getMessage ( ) ) ; return "" ; * 将图片转换成Base64编码的字符串 * @param path 图片本地路径 * @return base64编码的字符串 public static String imageToBase64 ( String path ) { if ( TextUtils . isEmpty ( path ) ) { return null ; InputStream is = null ; byte [ ] data ; String result = null ; try { is = new FileInputStream ( path ) ; //创建一个字符流大小的数组。 data = new byte [ is . available ( ) ] ; //写入数组 is . read ( data ) ; //用默认的编码格式进行编码 result = Base64 . encodeToString ( data , Base64 . DEFAULT ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } finally { if ( null != is ) { try { is . close ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; return result ; * 在ImageView里展示指定路径的图片 * @param path 本地路径 * @param imageView ImageView public static void ShowPic2View ( Context context , String path , ImageView imageView ) { File localFile ; FileInputStream localStream ; Bitmap bitmap ; localFile = new File ( path ) ; if ( ! localFile . exists ( ) ) { UtilsTools . MsgBox ( context , path + " is null." ) ; } else { try { localStream = new FileInputStream ( localFile ) ; bitmap = BitmapFactory . decodeStream ( localStream ) ; imageView . setImageBitmap ( bitmap ) ; // if (localStream != null) { localStream . close ( ) ; // } } catch ( Exception e ) { e . printStackTrace ( ) ; UtilsTools . MsgBox ( context , e . getMessage ( ) ) ; * 删除手机里指定路径的图片 * @param context Context * @param imgPath 本地路径 public static void DeletePicFromMobile ( Context context , String imgPath ) { try { ContentResolver resolver = context . getContentResolver ( ) ; Cursor cursor = MediaStore . Images . Media . query ( resolver , MediaStore . Images . Media . EXTERNAL_CONTENT_URI , new String [ ] { MediaStore . Images . Media . _ID } , MediaStore . Images . Media . DATA + "=?" , new String [ ] { imgPath } , null ) ; boolean result ; if ( cursor . moveToFirst ( ) ) { long id = cursor . getLong ( 0 ) ; Uri contentUri = MediaStore . Images . Media . EXTERNAL_CONTENT_URI ; Uri uri = ContentUris . withAppendedId ( contentUri , id ) ; int count = context . getContentResolver ( ) . delete ( uri , null , null ) ; result = count == 1 ; } else { File file = new File ( imgPath ) ; result = file . delete ( ) ; if ( result ) { UtilsTools . MsgBox ( context , "删除成功" ) ; } catch ( Exception e ) { UtilsTools . MsgBox ( context , e . getMessage ( ) ) ;

验证图片工具类的demo :

Activity :

package com.example.save_pic_delete;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.File;
public class MainActivity extends AppCompatActivity {
    private ImageView iv_main;
    private TextView tv_delete;
    private TextView tv_check;
    private Context mContext;
    private String filePath1;
    private String filePath;
    private String base64Str;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        mContext = this;
        // 原图路径
        filePath1 = "/storage/emulated/0/tencent/MicroMsg/WeiXin/mmexport1542093665881.jpg";
        // 展示原图
        ImageUtils.ShowPic2View(mContext, filePath1, iv_main);
        // 原图base64
        base64Str = ImageUtils.imageToBase64(filePath1);
        // base64的原图另存为到本项目路径
        filePath = ImageUtils.GenerateImage(mContext, base64Str);
    private void initView() {
        iv_main = findViewById(R.id.iv_main);
        tv_delete = findViewById(R.id.tv_delete);
        tv_check = findViewById(R.id.tv_check);
        // 删除
        tv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 删除本项目下保存的那个指定路径的图片
                ImageUtils.DeletePicFromMobile(mContext, filePath);
        });
        // 检测
        tv_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File file = new File(filePath);
                if (!file.exists()) {
                    UtilsTools.MsgBox_Long(mContext, "这个图片现在不存在");
                } else {
                    UtilsTools.MsgBox_Long(mContext, "这个图片存在");
        });

Activity的布局 : (只有一个显示base64转出图片的ImageView和一个删除按钮和一个检测本张图片还在不在本机的检测按钮)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <ImageView
        android:id="@+id/iv_main"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:src="@color/colorAccent"/>
    <TextView
        android:id="@+id/tv_delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="删除本地的这个图片"
        android:textStyle="bold"/>
    <TextView
        android:id="@+id/tv_check"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="检查本地的这个图片是否存在"
        android:textStyle="bold"/>
</LinearLayout>

其他没有注明的小类在其他文章中有贴过完整代码 :

其他拓展 : bitmap和base64互转 : https://blog.csdn.net/qq_28261207/article/details/79849444

图片工具类package com.example.save_pic_delete;import android.content.ContentResolver;import android.content.ContentUris;import android.content.Context;import android.database.Cursor;import android.g...
在我们开发应用的过程中,并不是所有情况下都请求图片的URL或者加载本地图片,有时我们需要加载Base64编码的图片。这种情况出现在服务端需要动态生成的图片,比如: 图形验证码 这些应用场景有个共同点就是,这些图片都是由服务器动态生成,并不需要生成后保存成文件再返回给客户端。 Android中ImageView加载Base64图片其实非常简单,并不需要引入第三方库,方法如下: imp...
String base64 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFAAAABXCAYAAACTFMIVAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAA..." byte[] decodedString = Base64.decode(base64, Base64.DEFAULT); Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedStr
2015年,我们在青云平台上实现了“百度云观测”应用。青云应用本质上是一个iframe,在向iframe服务方发送的请求中会携带一些数据,青云平台会使用Base64 URL对这些数据进行编码,其提供的编码解码算法示例如下: // php版本 function base64_URL_encode($data) { return rtrim(strtr(base64_encode($da...
public static String imageToBase64(String path){ if(TextUtils.isEmpty(path)){ return null; InputStream is = nu...
在 Java 中,可以使用 `Base64` 类对图片进行编码和解码。在将图片编码为 base64 字符串后,可以使用字符串操作来判断它是否是有效的 base64 编码。 具体来说,可以使用正则表达式来检查字符串是否符合 base64 编码规则,例如仅包含 A-Z、a-z、0-9、+、/ 以及 = 字符。下面是一个示例代码: public static boolean isValidBase64(String base64) { String base64Pattern = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$"; return base64.matches(base64Pattern); 还可以使用 apache commons codec 库里的 Base64.isBase64(byte[] base64) 来判断是否是base64字符串. import org.apache.commons.codec.binary.Base64; public static boolean isValidBase64(String base64) { return Base64.isBase64(base64.getBytes()); 这个方法返回值为布尔值,true表示是有效的base64字符串,false 表示无效。