Bild aus Galerie wählen oder mit Kamera schießen und hochladen

  • Antworten:6
Fränk R.
  • Forum-Beiträge: 9

17.06.2014, 19:53:19 via Website

Moin,
ich mache moment eine App wo ich per buttondruck ein Bild aus der Galerie aussuchen kann oder die Kamera öffnen kann.
Anschließend soll das Bild in ImageView gezeigt werden und per druck auf den uploadbutton auf meinen server hochgeladen werden.
Problem ist das ich den Pfad zu dem Bild nicht bekomme um es in die Uploadactivity zu übertragen.
Hier einmal mein code:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                File f = new File(Environment.getExternalStorageDirectory().toString());
                for (File temp : f.listFiles()) {
                    if (temp.getName().equals("temp.jpg")) {
                        f = temp;
                        break;
                    }
                }
                try {
                    Bitmap bitmap;
                    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

                    bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
                            bitmapOptions);

                    viewImage.setImageBitmap(bitmap);

                    String path = android.os.Environment
                            .getExternalStorageDirectory()
                            + File.separator
                            + "Phoenix" + File.separator + "default";
                    f.delete();
                    OutputStream outFile = null;
                    File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
                    try {
                        outFile = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
                        outFile.flush();
                        outFile.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (requestCode == 2) {

                Uri selectedImage = data.getData();
                String[] filePath = { MediaStore.Images.Media.DATA };
                Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null);
                c.moveToFirst();
                int columnIndex = c.getColumnIndex(filePath[0]);
                String picturePath = c.getString(columnIndex);
                c.close();
                Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
                Log.w("path of image from gallery......******************.........", picturePath+"");
                viewImage.setImageBitmap(thumbnail);
            }
        }

    }

}

zur upload klasse:

imageUrl = "picturePath";
uploadUrl = "htp://10.0.2.2/upoadandroid/upload.php";

     findViewById(R.id.senden).setOnClickListener(new View.OnClickListener() {

         @Override
         public void onClick(View v) {
         // TODO Auto-generated method stub
         String responce = new sendensave().uploadFile(uploadUrl, imageUrl);
         Toast.makeText(SendenActivity.this, responce,Toast.LENGTH_SHORT).show();
         }
         });
}

Problem ist jetzt, dass ich jetzt entweder einen pfad von der Gelerie oder von der Cam habe.
Wie übergebe ich jetzt den Galerie Pfad ODER den Image Pfad von der Cam, je nach dem was für eine aktion ausgeführt wird?

Ich würde mich wahnsinnig über eure Hilfe freuen!
Liebe Grüße!

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

17.06.2014, 22:09:32 via Website

Ich würde es einheitlich machen.
Erst das geschossene Bild speichern ( eingestellter Bilderpfad) und dann von dort aus hochladen. Dann ist es in etwa gleich wie aus der Galerie nämlich du hast einen Filename und den File willst du hochladen. Jetzt musst du nur irgendwie an den Pfad kommen.
Z.b. so:
http://stackoverflow.com/questions/16993213/how-can-i-find-out-the-camera-images-folder-of-an-android-phone

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Fränk R.
  • Forum-Beiträge: 9

18.06.2014, 14:03:49 via Website

Danke für die schnelle Antwort :)
Ich habe mir sonst gedacht mit getAbsolutePath(); beide pfade in den selben string zu übergeben.
das ganze soieht wie folgt aus:

Übergabe in uploadklasse (imageUrl):

findViewById(R.id.senden).setOnClickListener(new View.OnClickListener() {

         @Override
         public void onClick(View v) {
         // TODO Auto-generated method stub
         String responce = new sendensave().uploadFile(uploadUrl, imageUrl);
         Toast.makeText(SendenActivity.this, responce,Toast.LENGTH_SHORT).show();
         }
         });

In der onActivityResult hole ich mir die beiden pfade (imageUrl = f.getAbsolutePath)
Für Kamera:

if (requestCode == 1) {
                File f = new File(Environment.getExternalStorageDirectory().toString());
                imageUrl = f.getAbsolutePath();
                for (File temp : f.listFiles()) {
                    if (temp.getName().equals("temp.jpg")) {
                        f = temp;
                        break;

aber wie setze ich das jetzt im teil der Galerie um?

else if (requestCode == 2) {

                Uri selectedImage = data.getData();
                String[] filePath = { MediaStore.Images.Media.DATA };
                Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null);
                c.moveToFirst();
                int columnIndex = c.getColumnIndex(filePath[0]);
                String picturePath = c.getString(columnIndex);
                c.close();
                Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
                Log.w("path of image from gallery......******************.........", picturePath+"");
                viewImage.setImageBitmap(thumbnail);

Vielen Dank nochmal...
ich komm absolut nicht weiter und sehe mittlerweile nur noch zahlen und buchstaben^^

Antworten
Pascal P.
  • Admin
  • Forum-Beiträge: 11.286

18.06.2014, 16:36:46 via Website

In der Galerie hast du doch auch deinen Pfad.
Diesen übergibst du einfach deiner Upload Methode:
String responce = new sendensave().uploadFile(uploadUrl, imageUrl);

LG Pascal //It's not a bug, it's a feature. :) ;)

Antworten
Fränk R.
  • Forum-Beiträge: 9

18.06.2014, 17:02:06 via Website

-----

— geändert am 18.06.2014, 19:01:22

Antworten
Fränk R.
  • Forum-Beiträge: 9

18.06.2014, 18:58:57 via Website

Habe jetzt wieder den ganzen Tag alles mögliche versucht und komme zu keinem Abschluss.
Grade wenn ich denke das ich es hinbekommen hab kommt wieder ein Fehler hoch und ich bin wieder genau da wo ich vor einer Woche schon war.
Ich kann ein Bild aussuchen... von Galerie oder Cam und dieses in ImageView anzeigen lassen.
Über eine Woche versaue ich mir jetzt jeden Feierabend ausschließlich mit dieser scheiß Uploadfunktion.
Habe es mit Multpart entity versucht, da sagt er mir Multipartentity is deprecated... nehme ich einen anderen code, stimmt da auch wieder irgendwas nicht und ich bekomme es nicht ausgebessert.
Hier mein Code bis wo er funktioniert:

public class SendenActivity extends Activity {

Button senden;
Button b;
ImageView viewImage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.senden);
    viewImage=(ImageView)findViewById(R.id.ivimg);
    senden = (Button)findViewById(R.id.bsenden);
    b = (Button)findViewById(R.id.bbrowse);

    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            selectImage();
        }
    });
    findViewById(R.id.senden).setOnClickListener(new View.OnClickListener() {

         @Override
         public void onClick(View v) {
         // TODO Auto-generated method stub
         }
         });
}
  private void selectImage() {


        final CharSequence[] option = { "Kamera", "Galerie","Abbrechen" };

        AlertDialog.Builder builder = new AlertDialog.Builder(SendenActivity.this);
        builder.setTitle("Hochladen");
        builder.setItems(option, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                if (option[item].equals("Kamera"))
                {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); 
                    startActivityForResult(intent, 1);
                }
                else if (option[item].equals("Galerie"))
                {
                    Intent intent = new   Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    startActivityForResult(intent, 2);

                }
                else if (option[item].equals("Abbrechen")) {
                    dialog.dismiss();
                }
            }
        });
        builder.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                File f = new File(Environment.getExternalStorageDirectory().toString());
                for (File temp : f.listFiles()) {
                    if (temp.getName().equals("temp.jpg")) {
                        f = temp;
                        break;
                    }
                }
                try {
                    Bitmap bitmap;
                    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

                    bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
                            bitmapOptions);

                    viewImage.setImageBitmap(bitmap);

                    String path = android.os.Environment
                            .getExternalStorageDirectory()
                            + File.separator
                            + "Phoenix" + File.separator + "default";
                    f.delete();
                    OutputStream outFile = null;
                    File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
                    try {
                        outFile = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
                        outFile.flush();
                        outFile.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            } else if (requestCode == 2) {

                Uri selectedImage = data.getData();
                String[] filePath = { MediaStore.Images.Media.DATA };
                Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null);
                c.moveToFirst();
                int columnIndex = c.getColumnIndex(filePath[0]);
                String picturePath = c.getString(columnIndex);
                c.close();
                Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
                Log.w("path of image from gallery......******************.........", picturePath+"");
                viewImage.setImageBitmap(thumbnail);
            }
        }
    }

}

Das Layout:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="htp://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >

&lt;LinearLayout
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:orientation=&quot;vertical&quot; &gt;

&lt;TextView
    android:id=&quot;@+id/textView1&quot;
    android:layout_width=&quot;fill_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:text=&quot;@string/satzgalerie&quot; /&gt;

&lt;/LinearLayout&gt;


&lt;ScrollView
    android:id=&quot;@+id/scrollView1&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot; &gt;

&lt;LinearLayout
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:orientation=&quot;vertical&quot;
    android:gravity=&quot;center&quot; &gt;


    &lt;ImageView
        android:id=&quot;@+id/ivimg&quot;
        android:layout_width=&quot;200dp&quot;
        android:layout_height=&quot;100dp&quot;
        android:layout_marginTop=&quot;20dp&quot;
        android:contentDescription=&quot;@string/pic&quot;
        android:src=&quot;@drawable/bildrand&quot; /&gt;

    &lt;Button
        android:id=&quot;@+id/bbrowse&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginTop=&quot;15dp&quot;
        android:text=&quot;@string/durchsuchen&quot; /&gt;

    &lt;EditText
        android:id=&quot;@+id/edgps&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:hint=&quot;@string/hintort&quot;
        android:layout_marginTop=&quot;35dp&quot;
        android:ems=&quot;10&quot; /&gt;

    &lt;Button
        android:id=&quot;@+id/bsenden&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_marginTop=&quot;70dp&quot;
        android:text=&quot;@string/senden&quot; /&gt;

    &lt;/LinearLayout&gt;
&lt;/ScrollView&gt;

&lt;/LinearLayout&gt;

Ich möchte einfach nur das ausgewählte bild auf einen Server laden und evtl. noch einen Text der im EditText Feld steht dazu.
Mir macht das lernen und Programmieren ja spaß und ich lerne gerne mehr auf diesem Gebiet.
Aber ich hab echt keine lust weitere 2 Wochen meinen Feierabend damit zu verbringen, nichts zustande zu kriegen.
Von mir aus bekommt der jenige auch Geld dafür... hauptsache es geht endlich mal wieder weiter.

Vielen Dank!

— geändert am 18.06.2014, 19:00:56

Antworten
Fränk R.
  • Forum-Beiträge: 9

19.06.2014, 17:48:44 via Website

Kann mir da denn keiner helfen?

Antworten