String sql = "insert into ports (imgdat) values ('" +sendData + "')";
ここのsendDataに
Reports any arrays used in String concatenations or as parameters to java.io.PrintStream methods (such as System.out.println()). Usually in such a case, the contents of the array were meant to be used and the not array object itself.
このようなエラーがでます。。。。
バイナリデータを保存するやり方ご教授お願いします!!
java
1package com.example.ryo.testtest; 2import android.app.Activity; 3import android.content.Context; 4import android.content.Intent; 5import android.graphics.Bitmap; 6import android.net.Uri; 7import android.provider.MediaStore; 8import android.support.v7.app.AppCompatActivity; 9import android.os.Bundle; 10import android.util.Log; 11import android.view.View; 12import android.widget.Button; 13import android.widget.ImageView; 14import java.sql.*; 15import java.io.*; 16import java.io.File; 17import java.io.FileInputStream; 18import java.sql.Connection; 19import java.sql.PreparedStatement; 20import java.sql.ResultSet; 21import java.sql.Statement; 22import java.sql.DriverManager; 23import android.widget.TextView; 24import android.os.AsyncTask; 25import android.view.View.OnClickListener; 26import android.widget.Toast; 27 28import java.io.ByteArrayOutputStream; 29import java.io.IOException; 30 31import static android.content.ContentValues.TAG; 32 33public class MainActivity extends AppCompatActivity implements OnClickListener { 34 private static final int READ_REQUEST_CODE = 42; 35 ImageView imageView; 36 byte[] sendData; 37 38 @Override 39 protected void onCreate(Bundle savedInstanceState) { 40 super.onCreate(savedInstanceState); 41 setContentView(R.layout.activity_main); 42 Button button2 = findViewById(R.id.button2); 43 Button button1 = this.findViewById(R.id.button1); 44 button1.setOnClickListener(this); 45 imageView = findViewById(R.id.imageView); 46 47 button2.setOnClickListener(new View.OnClickListener() { 48 //暗黙的Intentを使ってギャラリーアプリを選択し、画像のURIを取得 49 @Override 50 public void onClick(View v) {//ギャラリーから画像を選択するため 51 Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 52 intent.setType("image/*"); 53 startActivityForResult(intent, READ_REQUEST_CODE); 54 } 55 }); 56 57 } 58 //実際の画像(Bitmap)の取得 59 @Override//コンパイラのエラーなどを教えてくれる 60 public void onActivityResult(int requestCode, int resultCode, 61 Intent resultData) { 62 63 if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) { 64 Uri uri = null; 65 if (resultData != null) { 66 uri = resultData.getData(); 67 try { 68 Bitmap bmp = MediaStore.Images.Media.getBitmap(getContentResolver(), uri); 69 imageView.setImageBitmap(bmp); 70 //画像をバイナリ化している 71 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 72 bmp.compress(Bitmap.CompressFormat.PNG, 100, baos); 73 byte[] sendData = baos.toByteArray(); 74 75 76 } catch (IOException e) { 77 e.printStackTrace(); 78 } 79 } 80 } 81 } 82 83 84 85 86 //後で書く 87 @Override 88 public void onClick(View v) { 89 if (v.getId() == R.id.button1) { // ボタン1が押下されたら 90 TaskDbInsert task1 = new TaskDbInsert(MainActivity.this); 91 task1.execute(); 92 } 93 } 94 95 96 97 class TaskDbInsert extends AsyncTask<Void, Void, Void> { 98 Activity activity = null; 99 100 // コンストラクタ 101 public TaskDbInsert(MainActivity act) { 102 activity = act; 103 } 104 105 @Override 106 protected Void doInBackground(Void... voids) { 107 //入力部の定義 108 109 try { 110 Class.forName("com.mysql.jdbc.Driver"); 111 Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/images", "root", "PASSWORD"); 112 Statement stmt = conn.createStatement(); 113 String sql = "insert into ports (imgdat) values ('" +sendData + "')"; 114 stmt.executeUpdate(sql); 115 stmt.close(); 116 conn.close(); 117 } catch (Exception e) { 118 Log.e(TAG, "doInBackground:", e); 119 } 120 return null; 121 } 122 protected void onPostExecute(){ 123 Toast.makeText(activity,"登録を終了しました", Toast.LENGTH_LONG).show(); 124 } 125 } 126 127 128}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/20 07:36
2017/11/20 08:20 編集
2017/11/20 08:20
2017/11/20 08:24
2017/11/20 08:28 編集
2017/11/20 08:34 編集
2017/11/20 08:42
2017/11/20 12:25
2017/11/20 13:16
2017/11/20 13:50 編集
2017/11/20 15:33
2017/11/20 16:10 編集
2017/11/21 00:01
2017/11/21 00:04
2017/11/25 18:27
2017/11/27 00:26
2017/11/27 04:49
2017/12/01 07:38