ディスクから画像を読み取る必要がありますが、 FileNotFoundException があります。
try {
fileInputStream = new FileInputStream(new File("C:/wakaicon.png"));
} catch(FileNotFoundException e) {
Log.e("DEBUG", "[Picture Not Found]");
}
ディスクから画像を読み取る必要がありますが、 FileNotFoundException があります。
try {
fileInputStream = new FileInputStream(new File("C:/wakaicon.png"));
} catch(FileNotFoundException e) {
Log.e("DEBUG", "[Picture Not Found]");
}
問題の解決策が見つかりました... 1. SD カードを作成します (Edit AVD を使用) 2. ファイル エクスプローラーを開きます (IDE -> ウィンドウ -> ビューの表示 -> ファイル エクスプローラー) 3. 画像を SD カードに追加しますディレクトリ
package com.example.wakauploadpicture;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
public class handlePicture {
handlePicture(){}
//
@SuppressWarnings("deprecation")
public boolean handlePicture(String filePath, String mimeType) {
HttpURLConnection connection = null;
DataOutputStream outStream = null;
DataInputStream inStream = null;
String lineEnd = "-----------------------------16572156320853";
String twoHyphens = "--";
String boundary = "---------------------------29463111463415";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
String urlString = "http://192.168.224.37:8081/waUpload/upload/uploadHandlers.js";
Log.v("State : ",Environment.getExternalStorageState());
Log.v("",Environment.getExternalStorageDirectory().getAbsolutePath());
final File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath(), "wakaicon.png");
try {
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
} catch(FileNotFoundException e) {
Log.e("DEBUG", "[Picture Not Found]");
}
URL url = new URL(urlString);
connection = (HttpURLConnection) url.openConnection();
//indicates that the application intends to read data from the URL connection.
connection.setDoInput(true);
//indicates that the application intends to write data to the URL connection.
connection.setDoOutput(true);
connection.setUseCaches(true);
//Set the method for the URL request, one of: POST
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
connection.setRequestProperty("Accept-Encoding", "gzip, deflate");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data");
connection.setRequestProperty("Referer", "http://127.0.0.1:8081/index.waPage/index.html");
//connection.setRequestProperty("Content-Length", ""+file.length());
Log.v("Content-Length", ""+file.length());
outStream = new DataOutputStream(connection.getOutputStream());
outStream.writeBytes(boundary);
outStream.writeBytes("Content-Disposition: form-data; name=\"filesToUpload\"; filename=\"" + "wakaicon.png");
outStream.writeBytes("Content-Type: "+ mimeType);
//
// bytesAvailable = fileInputStream.available();
// bufferSize = Math.min(bytesAvailable, maxBufferSize);
// buffer = new byte[bufferSize];
//
// bytesRead = fileInputStream.read(buffer, 0, bufferSize);
//
// while (bytesRead > 0) {
// outStream.write(buffer, 0, bufferSize);
// Log.v("Buffer : ",buffer.toString());
// bytesAvailable = fileInputStream.available();
// bufferSize = Math.min(bytesAvailable, maxBufferSize);
// bytesRead = fileInputStream.read(buffer, 0, bufferSize);
// outStream.write(bytesRead);
// }
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap bm = BitmapFactory.decodeStream(fis);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100 , baos);
byte[] b = baos.toByteArray();
for(int i=0;i<b.length;i++){
Log.v("byte : ",""+b[i]);
outStream.write(b[i]);
}
outStream.write(getBytesFromBitmap(bm));
outStream.writeBytes(twoHyphens+boundary);
outStream.writeBytes("Content-Disposition: form-data; name=\"config\"");
outStream.writeBytes("{\"folder\":\"tmp\",\"replace\":false,\"datasource\":{\"dsname\":\"Emloyee\",\"id\":\"5\",\"field\":\"photo\",\"saveOnDS\":true}}");
outStream.writeBytes(boundary+twoHyphens);
fileInputStream.close();
outStream.flush();
outStream.close();
connection.disconnect();
} catch (MalformedURLException e) {
Log.e("DEBUG", "[MalformedURLException while sending a picture]");
} catch (IOException e) {
Log.e("DEBUG", "[IOException while sending a picture]");
}
try {
String sHeaderValue = connection.getResponseMessage();
Log.v("Response : ",sHeaderValue);
inStream = new DataInputStream( connection.getInputStream() );
String str;
while (( str = inStream.readLine()) != null) {
Log.v("str pic :",str);
if(str=="1") {
return true;
} else {
return false;
}
}
inStream.close();
} catch (IOException e){
Log.e(e.toString(), "[IOException while sending a picture and receiving the response]");
}
return false;
}
public byte[] getBytesFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 70, stream);
return stream.toByteArray();
}
}