Php 如何利用安卓毕加索在服务器的非公开html位置加载图像?
在安卓应用程序中,要加载毕加索的图像,需要图像的URL,但我想将图像放在服务器的非公共html位置,那里没有可用的URL 如何利用安卓毕加索在服务器的非公开html位置加载图像 注释中请求的代码:Php 如何利用安卓毕加索在服务器的非公开html位置加载图像?,php,android,Php,Android,在安卓应用程序中,要加载毕加索的图像,需要图像的URL,但我想将图像放在服务器的非公共html位置,那里没有可用的URL 如何利用安卓毕加索在服务器的非公开html位置加载图像 注释中请求的代码: 服务器的Android上传和与PHP通信代码如下: public SendJSONToServer(Context context,String FileNewName,String url,String uri, SendJSONToServer.OnEventListener callback )
服务器的
Android
上传和与PHP
通信代码如下:
public SendJSONToServer(Context context,String FileNewName,String url,String uri, SendJSONToServer.OnEventListener callback ) {
this.url = url;
this.uri = uri;
this.FileNewName = FileNewName;
mCallBack = callback;
mContext = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
if (checkNetworkConnection() == false)
return null;
try {
JSONHttpHandler3 sh = new JSONHttpHandler3();
String srv_RC ;
srv_RC = sh.makeServiceCall3(FileNewName,url,uri);
if (srv_RC.equals("Move To Server Failed"))
{
return null;
}
else if (srv_RC.equals("Move To Server successful"))
{
return "Move To Server successful";
}
else
{
return srv_RC;
}
}
catch (Exception e)
{
mException = e;
}
return null;
}
@Override
protected void onPostExecute(String result) {
if (mCallBack != null) {
if (mException == null) {
if(result == null)
{
mCallBack.onFailure(null);
}
else
{
mCallBack.onSuccess(result);
}
} else {
mCallBack.onFailure(null);
}
}
}
public interface OnEventListener<T> {
public void onSuccess(T object);
public void onFailure(String f);
}
public boolean checkNetworkConnection() {
ConnectivityManager connMgr = (ConnectivityManager)
mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
boolean isConnected = false;
if (networkInfo != null && (isConnected = networkInfo.isConnected())) {
}
else {
}
return isConnected;
}
对于PHP代码的文件访问,以下代码是处理文件的示例(这是JSON示例,但也可以是图像):
您能为我提供帮助链接或示例代码吗?
public String makeServiceCall3(String FileNewName,String FileDestination,String sourceFileUri) {
int serverResponseCode = 0;
String fileName = FileNewName;
String upLoadServerUri = null;
/************* Php script path ****************/
upLoadServerUri = FileDestination;//"http://www.koalaapp.ir/DB/upload3.php";
HttpURLConnection conn = null;
DataOutputStream dos = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 5 * 1024 * 1024;
File sourceFile = new File(sourceFileUri);
if (!sourceFile.isFile()) {
Log.e("uploadFile", "Source File not exist :" + sourceFileUri);
return "0";
}
else {
try {
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(upLoadServerUri);
// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("uploaded_file", fileName);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\"; filename=\""
+ fileName + "\"" + lineEnd);
int length = fileInputStream.available();
Log.e(TAG, "lllllllllllllllllll: "+length);
dos.writeBytes(lineEnd);
// create a buffer of maximum size
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder total = new StringBuilder();
String line;
while ((line = in.readLine()) != null)
{
total.append(line).append('\n');
}
fileInputStream.close();
dos.flush();
dos.close();
if (total.toString().contains("Move successful") == true )
{
return "Move To Server successful";
}
else
{
return "Move To Server Failed";
}
} catch (MalformedURLException ex) {
ex.printStackTrace();
return "Move To Server Failed";
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "111Exception: " + e.getMessage());
return "Move To Server Failed";
}
}
} // End else block
<?php
$myfile = fopen("j000000033w.json", "w") or die("Unable to open file!");
$txt = "John Doe\n";
fwrite($myfile, $txt);
$txt = "Jane Doe\n";
fwrite($myfile, $txt);
fclose($myfile);
?>