ユーザーがギャラリーまたはカメラから画像を閲覧できるAndroidアプリケーションを作成する必要があります..画像は文字列に変換され、ローカルサーバーにアップロードされます。サーバーでは、画像が再度変換され、画像として保存されます。画像を圧縮しましたが、アップロードできません 助けてください
以下はコードです..
package com.example.hdfcuploadapp;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import android.util.Base64;
import android.app.Activity;``
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
public class MainActivity extends Activity {
private static final int REQ_CODE_PICK_IMAGE = 1;
private static final int CAMERA_REQUEST = 1888;
private static View popupView = null;
private static PopupWindow popupWindow = null;
private static Button buttonBrowse = null;
private static String compressedImage = null;
private static Point position = null;
private static Bitmap imageSelected = null;
private static String selectedImageRealPath = null;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttonUpload = (Button) findViewById(R.id.buttonUpload);
buttonUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
buttonBrowse = (Button) findViewById(R.id.buttonBrowse);
buttonBrowse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(position!= null){
showPopup(MainActivity.this, position);
}
}
@SuppressWarnings("deprecation")
private void showPopup(final Activity activity, Point position) {
int popupWidth = 120;
int popupHeight = 130;
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = layoutInflater.inflate(R.layout.popup, linearLayout);
popupWindow = new PopupWindow(activity);
popupWindow.setContentView(popupView);
popupWindow.setWidth(popupWidth);
popupWindow.setHeight(popupHeight);
int offset_X = 85;
int offset_Y = 5;
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.showAtLocation(popupView, Gravity.NO_GRAVITY, position.x + offset_X, position.y + offset_Y);
}
});
}
@Override
public void onWindowFocusChanged(boolean hasFocus){
int[] location = new int[2];
buttonBrowse.getLocationOnScreen(location);
position = new Point();
position.x = location[0];
position.y = location[1];
}
public void buttonGallery_Click(View v){
Intent intent = new Intent(
Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, REQ_CODE_PICK_IMAGE);
}
public void buttonTakePhoto_Click(View v){
popupWindow.dismiss();
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent){
switch(requestCode) {
case REQ_CODE_PICK_IMAGE:
if(resultCode == RESULT_OK){
Uri selectedImageUri = intent.getData();
selectedImageRealPath = getRealPathFromURI(selectedImageUri);
imageSelected = BitmapFactory.decodeFile(selectedImageRealPath);
compressedImage = imageCompression(selectedImageRealPath);
final ImageView imageViewPhoto = (ImageView) findViewById(R.id.imageViewPhoto);
imageViewPhoto.setImageBitmap(imageSelected);
}
break;
case CAMERA_REQUEST:
if(resultCode == RESULT_OK){
Uri selectedImageUri = intent.getData();
selectedImageRealPath = getRealPathFromURI(selectedImageUri);
imageSelected = (Bitmap) intent.getExtras().get("data");
MediaStore.Images.Media.insertImage(getContentResolver(), imageSelected, String.valueOf(System.currentTimeMillis()), "This is an Image.");
compressedImage = imageCompression(selectedImageRealPath);
final ImageView imageViewPhoto = (ImageView) findViewById(R.id.imageViewPhoto);
imageViewPhoto.setImageBitmap(imageSelected);
}
break;
}
popupWindow.dismiss();
}
private String imageCompression(String filePath) {
File imageFile = new File(filePath);
FileInputStream fis = null;
try
{
fis = new FileInputStream(imageFile);
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
Bitmap bm = BitmapFactory.decodeStream(fis);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 90, baos );
byte[] b = baos.toByteArray();
return Base64.encodeToString(b, Base64.DEFAULT);
}
public String getRealPathFromURI(Uri contentUri)
{
try
{
String[] proj = {MediaStore.Images.Media.DATA};
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
catch (Exception e)
{
return contentUri.getPath();
}
}
}