Я пытаюсь отправить изображение через скрипт PHP, поэтому ImageView
изменения на клиенте Android.
На данный момент я получаю это и так без изменения изображения.
D/skia: --- SkAndroidCodec::NewFromStream returned null
Любая помощь будет с благодарностью исправить это. Ура 🙂
Вот мой код Android:
public class MainActivity extends AppCompatActivity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
}
private class getImage extends AsyncTask<String, String, String> {
HttpURLConnection conn;
URL url = null;
public getImage(){
}
@Override
protected String doInBackground(String... params) {
Log.d("TAG", "CALLED");
try {
// Enter URL address where your php file resides
url = new URL(<absolute_path_to_file>);
} catch (MalformedURLException e) {
e.printStackTrace();
return e.toString();
}
try {
// Setup HttpURLConnection class to send and receive data from php and mysql
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
// setDoInput and setDoOutput to true as we send and recieve data
conn.setDoInput(true);
conn.setDoOutput(true);
// add parameter to our above url
String weekDay;
SimpleDateFormat dayFormat = new SimpleDateFormat("EEEE", Locale.ENGLISH);
Calendar calendar = Calendar.getInstance();
weekDay = dayFormat.format(calendar.getTime());
Uri.Builder builder = new Uri.Builder().appendQueryParameter("item_index", "FOUR");
String query = builder.build().getEncodedQuery();
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(query);
writer.flush();
writer.close();
os.close();
conn.connect();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
return e1.toString();
}
try {
int response_code = conn.getResponseCode();
// Check if successful connection made
if (response_code == HttpURLConnection.HTTP_OK) {
// Read data sent from server
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
// Pass data to onPostExecute method
return (result.toString());
} else {
return("Connection error");
}
} catch (IOException e) {
e.printStackTrace();
return e.toString();
} finally {
conn.disconnect();
}
}
@Override
protected void onPostExecute(String result) {
InputStream is;
try{
is = conn.getInputStream();
//get image from server
Bitmap bitmap = BitmapFactory.decodeStream(is);
imageView.setImageBitmap(bitmap);
is.close();
} catch (IOException e){
Log.d("TAG", "ERROR: " + e);
}
}
}
}
Вот мой скрипт PHP-сервера:
<?php
if(isset($_POST['item_index'])){
$filename = "leo.jpg";
$location = "leo.jpg";
$mimeType="application/octet-stream";
if(!file_exists($location))
{ header ("HTTP/1.0 404 Not Found");
return;
}
$size=filesize($location);
$time=date('r',filemtime($location));
#html response header
header('Content-Description: File Transfer');
header("Content-Type: $mimeType");
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: no-cache');
header('Accept-Ranges: bytes');
header('Content-Length:'.($size));
header("Content-Disposition: inline; filename=$filename");
header("Content-Transfer-Encoding: binary\n");
header("Last-Modified: $time");
header('Connection: close');
ob_clean();
flush();
readfile($location);
}
?>
Задача ещё не решена.
Других решений пока нет …