1.在存储卡上读写图片文件
1.1activity_image_write.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/btn_save" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="保存"/> <Button android:id="@+id/btn_read" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="读取"/> <ImageView android:id="@+id/iv_content" android:layout_width="match_parent" android:layout_height="400dp" android:scaleType="fitCenter" /> </LinearLayout>
1.2ImageWriteActivity.java
package com.example.chapter06; import androidx.appcompat.app.AppCompatActivity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.View; import android.widget.ImageView; import com.example.chapter06.util.FileUtil; import com.example.chapter06.util.ToastUtil; import java.io.File; public class ImageWriteActivity extends AppCompatActivity implements View.OnClickListener{ private String path; private ImageView iv_content; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image_write); iv_content = findViewById(R.id.iv_content); findViewById(R.id.btn_save).setOnClickListener(this); findViewById(R.id.btn_read).setOnClickListener(this); } @Override public void onClick(View v) { switch(v.getId()) { case R.id.btn_save: String fileName = System.currentTimeMillis() + ".jpeg"; // 内部存储私有空间 path = getFilesDir().toString() + File.separatorChar + fileName; Log.d("ning", path); // 从指定的资源文件中获取位图对象 Bitmap b1 = BitmapFactory.decodeResource(getResources(), R.drawable.ting1); // 把位图对象保存为图片文件 FileUtil.saveImage(path, b1); ToastUtil.show(this, "保存成功"); break; case R.id.btn_read: // Bitmap b2 = FileUtil.openImage(path); // iv_content.setImageBitmap(b2); // Bitmap b2 = BitmapFactory.decodeFile(path); // iv_content.setImageBitmap(b2); // 直接调用setImageURI方法,设置图像视图的路径对象 iv_content.setImageURI(Uri.parse(path)); break; } } }
1.3FileUtil.java
package com.example.chapter06.util; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class FileUtil { // 把字符串保存到指定路径的文本文件 public static void saveText(String path, String txt) { BufferedWriter os = null; try { os = new BufferedWriter(new FileWriter(path)); os.write(txt); } catch (Exception e) { e.printStackTrace(); } finally { if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } } // 从指定路径的文本文件中公读取内容字符串 public static String openText(String path) { BufferedReader is = null; StringBuilder sb = new StringBuilder(); try { is = new BufferedReader(new FileReader(path)); String line = null; while((line = is.readLine()) != null) { sb.append(line); } } catch (Exception e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } return sb.toString(); } // 把位图数据保存到指定路径的图片文件 public static void saveImage(String path, Bitmap bitmap) { FileOutputStream fos = null; try { fos = new FileOutputStream(path); // 把位图数据压缩到文件输出流中 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } // 从指定路径的图片文件中读取位图数据 public static Bitmap openImage(String path) { Bitmap bitmap = null; FileInputStream fis = null; try { fis = new FileInputStream(path); bitmap = BitmapFactory.decodeStream(fis); } catch (Exception e) { e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } return bitmap; } }
1.4效果:
点击“保存”:
控制台输出:
D/ning: /data/user/0/com.example.chapter06/files/1667433855229.jpeg
验证:
点击“读取”:
“读取”按钮点击事件里,注释掉的两种方法,与上图效果相同,已验证,就不截图了。
2.Application生命周期
2.1MyApplication.java
package com.example.chapter06; import android.app.Application; import android.content.res.Configuration; import android.util.Log; import androidx.annotation.NonNull; public class MyApplication extends Application { // 在APP启动时调用 @Override public void onCreate() { super.onCreate(); Log.d("ning", "MyApplication onCreate"); } // 在APP终止时调用 @Override public void onTerminate() { super.onTerminate(); Log.d("ning", "onTerminate"); } // 在配置改变时调用,例如从竖屏变为横屏 @Override public void onConfigurationChanged(@NonNull Configuration newConfig) { super.onConfigurationChanged(newConfig); Log.d("ning", "onConfigurationChanged"); } }
2.2MainActivity.java
package com.example.chapter06; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d("ning", "MainActivity onCreate"); } }
2.3AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.chapter06"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application android:name=".MyApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:requestLegacyExternalStorage="true" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.MyApplication"> <activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".LoginForgetActivity" /> </application> </manifest>
2.4效果:
启动APP后控制台输出:
2022-11-03 19:05:51.491 3361-3361/com.example.chapter06 D/ning: MyApplication onCreate 2022-11-03 19:05:52.786 3361-3361/com.example.chapter06 D/ning: MainActivity onCreate
旋转手机后,控制台输出:
2022-11-03 19:09:33.808 3361-3361/com.example.chapter06 D/ning: onConfigurationChanged 2022-11-03 19:09:33.999 3361-3361/com.example.chapter06 D/ning: onConfigurationChanged 2022-11-03 19:09:34.475 3361-3361/com.example.chapter06 D/ning: MainActivity onCreate
屏幕:
标签:11,chapter06,java,学习,2022,import,path,android,public From: https://www.cnblogs.com/pingfanliliang/p/16853150.html