我需要从光盘中读取一张图片,但我有FileNotFoundException!
try {
fileInputStream = new FileInputStream(new File("C:/wakaicon.png"));
} catch(FileNotFoundException e) {
Log.e("DEBUG", "[Picture Not Found]");
}
答案 0 :(得分:0)
在android中,我们无法从光盘的任何部分获取文件。您必须在模拟器或设备中创建外部存储,并且必须提供类似“mnt / sdcard / yourfolder / yourfile。”的路径。
希望这会对你有所帮助。
你也可以这样试试。
File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "path/to/the/file/inside/the/sdcard.ext");
答案 1 :(得分:0)
模拟器是PC上的一个独立虚拟机 - adb是将文件推送到外部SD卡或其他目录的解决方案(这里是指南:http://developer.android.com/tools/help/adb.html)。 您无法在应用中访问您的硬盘。要访问外部目录中的文件,请尝试以下代码:
getExternalStorageDirectory ()
它返回一个文件。以下是有关它的文档:http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()
编辑: 您可以将带有adb的文件推送到外部SD卡,然后您可以尝试类似
的内容File env = Enviroment.getExternalStorageDirectory()
File folder = new File(env,"Pictures/yourNewPic")
if(!folder.exists()){folder.mkdirs()}
需要<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> in your manifest.
答案 2 :(得分:0)
我找到了解决问题的方法...... 1.创建SD卡(通过编辑AVD) 2.打开文件资源管理器(IDE - &gt;窗口 - &gt;显示视图 - &gt;文件浏览) 3.将图片添加到SD CARD目录
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();
}
}