以下の事が解決しません、どなたかご教示願います。
##やりたいこと
①「読込」ボタンを押すとギャラリーを呼び出し、画像を読み込み表示する
②「処理」ボタンを押すと読み込んだ画像が加工処理され、表示される
##できないところ
エミュレータは起動できた。
①はできた。
②のボタンを押した瞬間エラー表記が出てエミュレータが閉じる。
///画素読み込み/////のところのbmpにエラーが生じてしまい
解決できない。
##ソースコード
java
1public class MainActivity extends AppCompatActivity { 2 3 private static final int RESULT_PICK_IMAGEFILE = 1000; 4 ImageView imageView; 5 6 int width; 7 int height; 8 Bitmap bmp = null; 9 10 @Override 11 protected void onCreate(Bundle savedInstanceState) { 12 super.onCreate(savedInstanceState); 13 setContentView(R.layout.activity_main); 14 15 Button button = findViewById(R.id.button1); //読込 16 Button button2 = findViewById(R.id.button3); //処理開始 17 18 Bitmap bmp = BitmapFactory.decodeResource( 19 MainActivity.this.getResources(), 20 R.drawable.gazou 21 ); 22 23 ((ImageView) findViewById(R.id.imageView)).setImageBitmap(bmp); 24 25////////////////////////////////////読み込みボタン///////////////////////////////////////////////// 26 button.setOnClickListener(new View.OnClickListener() { 27 @Override 28 public void onClick(View view1) { 29 Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); 30 intent.addCategory(Intent.CATEGORY_OPENABLE); 31 intent.setType("image/*"); 32 33 //onActivityResultを起動 34 startActivityForResult(intent, RESULT_PICK_IMAGEFILE); 35 36 } 37 }); 38///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 39 40 41 42//////////////////////////////////////////処理開始ボタン//////////////////////////////////////////////////////////////////// 43 button2.setOnClickListener(new View.OnClickListener() { 44 @Override 45 public void onClick(View view2) { 46 47 /////画素読み込み/////////////////////////////////////////// 48 width = bmp.getWidth(); 49 height = bmp.getHeight(); 50 //画像処理用配列 51 int[] pixels = new int[width*height]; 52 //pixelsの配列にmyBitmapのデータを格納する 53 bmp.getPixels(pixels, 0, width, 0, 0, width, height); 54 /////////////////////////////////////////////////////////// 55 56 //出力用画像領域確保 57 Bitmap output=Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 58 59 /////画像加工部分///// 60 //省略 61 ///////////////////// 62 63 //出力用の領域にセットする 64 output.setPixels(pixels, 0, width, 0, 0, width, height); 65 66 ((ImageView) findViewById(R.id.imageView)).setImageBitmap(output); 67 68 69 } 70 }); 71///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 72 73 } 74 75///////////////////////////////////////////////画像読み込み処理/////////////////////////////////////////////////// 76 @Override 77 public void onActivityResult(int requestCode, int resultCode, Intent resultData) { 78 if (requestCode == RESULT_PICK_IMAGEFILE && resultCode == Activity.RESULT_OK) { 79 if (resultData.getData() != null) { 80 81 ParcelFileDescriptor pfDescriptor = null; 82 try { 83 Uri uri = resultData.getData(); 84 pfDescriptor = getContentResolver().openFileDescriptor(uri, "r"); 85 if (pfDescriptor != null) { 86 FileDescriptor fileDescriptor = pfDescriptor.getFileDescriptor(); 87 Bitmap bmp = BitmapFactory.decodeFileDescriptor(fileDescriptor); 88 pfDescriptor.close(); 89 90 ((ImageView) findViewById(R.id.imageView)).setImageBitmap(bmp); 91 } 92 } catch (IOException e) { 93 e.printStackTrace(); 94 } finally { 95 try { 96 if (pfDescriptor != null) { 97 pfDescriptor.close(); 98 } 99 } catch (Exception e) { 100 e.printStackTrace(); 101 } 102 } 103 104 } 105 } 106 } 107///////////////////////////////////////////////////////////////////////////////////////////////////////////////// 108}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。