Android中查看圖片

Bitmap基本概念:

  1. bitmap是Android系統中的圖像處理的重要類之一;
  2. 通過bitmap我們可以獲取到圖片的相關信息;
  3. bitmap文件圖像效果好就需要佔用越大存儲空間;
//查看圖片
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private EditText et_path;
    private ImageView iv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_path = findViewById(R.id.et_path);
        iv = findViewById(R.id.iv);
    }

    public void click(View v) {
        new Thread() {                                 //網絡操作不能在UI線程中
            public void run() {
                try {
                    File file = new File(getCacheDir(), "test.png");
                    if (file.exists() && file.length() > 0) {
                        final Bitmap cacheBitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                        runOnUiThread(new Runnable() {             //UI操作要在UI線程中執行
                            @Override
                            public void run() {
                                iv.setImageBitmap(cacheBitmap);
                            }
                        });
                    }

                    String path = et_path.getText().toString().trim();
                    
                    URL url = new URL(path);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(5000);
                    if (connection.getResponseCode() == 200) {
                        InputStream inputStream = connection.getInputStream();

                        FileOutputStream fileOutputStream = new FileOutputStream(file);
                        int len;
                        byte[] buffer = new byte[1024];
                        while ((len = inputStream.read(buffer)) != -1) {
                            fileOutputStream.write(buffer, 0, len);
                        }
                        fileOutputStream.close();
                        inputStream.close();
                        
                        final Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
                        runOnUiThread(new Runnable() {    //UI操作要在UI線程中執行
                            @Override
                            public void run() {
                                iv.setImageBitmap(bitmap);
                            }
                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}

Android Bitmap深入介紹
玩轉Adnroid Bitmap

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章