Bild aus Gallery holen

  • Antworten:4
  • Bentwortet
Kinq__#
  • Forum-Beiträge: 47

15.09.2013, 18:22:32 via Website

Hallo zusammen, ich habe ein kleines Problem habe schon verzweifeld gesucht aber nichts gefunden. Bei einem Android 2.3 funktioniert das aussuchen und uploaden eines Bildes fantastisch, aber ist es ein Android 4.1 kommt beim auswählen eines Bildes das die Activity angehalten wurde, ich hoffe ihr könnt mir helfen. ;)
Ich habe den Folgenden Code
einmal gallery öffnen
1btngallery.setOnClickListener(new View.OnClickListener() {
2
3 @Override
4 public void onClick(View arg0) {
5 // TODO Auto-generated method stub
6 //openGallery(SELECT_FILE1);
7 Intent i = new Intent(
8 Intent.ACTION_PICK,
9 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
10
11 startActivityForResult(i, RESULT_LOAD_IMAGE);
12 }
13 });
Dann noch den rest
1@Override
2 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
3 super.onActivityResult(requestCode, resultCode, data);
4
5 if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
6 Uri selectedImage = data.getData();
7 String[] filePathColumn = { MediaStore.Images.Media.DATA };
8
9 Cursor cursor = getContentResolver().query(selectedImage,
10 filePathColumn, null, null, null);
11 cursor.moveToFirst();
12
13 int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
14 String picturePath = cursor.getString(columnIndex);
15 cursor.close();
16
17 ImageView imageView = (ImageView) findViewById(R.id.image);
18 imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
19 selectedPath1 = getPath(selectedImage);
20 File imgFile = new File(selectedPath1);
21 if(imgFile.exists()){
22
23 Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
24
25 ImageView myImage = (ImageView) findViewById(R.id.image);
26 myImage.setImageBitmap(myBitmap);
27 //Upload
28 HttpURLConnection conn = null;
29 DataOutputStream dos = null;
30 DataInputStream inStream = null;
31 String exsistingFileName = selectedPath1;
32 // Is this the place are you doing something wrong.
33 String lineEnd = "\r\n";
34 String twoHyphens = "--";
35 String boundary = "*****";
36 int bytesRead, bytesAvailable, bufferSize;
37 byte[] buffer;
38 int maxBufferSize = 1*1024*1024;
39 session = new SessionManager(getApplicationContext());
40 HashMap<String, String> user = session.getUserDetails();
41 String username = user.get(SessionManager.KEY_ID);
42 String urlString = "http://*******************/upload_image.php?username="+username;
43 try
44 {
45
46 FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName) );
47 URL url = new URL(urlString);
48 conn = (HttpURLConnection) url.openConnection();
49 conn.setDoInput(true);
50 // Allow Outputs
51 conn.setDoOutput(true);
52 // Don't use a cached copy.
53 conn.setUseCaches(false);
54 // Use a post method.
55 conn.setRequestMethod("POST");
56 conn.setRequestProperty("Connection", "Keep-Alive");
57 conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
58 dos = new DataOutputStream( conn.getOutputStream() );
59 dos.writeBytes(twoHyphens + boundary + lineEnd);
60 dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + exsistingFileName +"\"" + lineEnd);
61 dos.writeBytes(lineEnd);
62 Log.e("MediaPlayer","Headers are written");
63 bytesAvailable = fileInputStream.available();
64 bufferSize = Math.min(bytesAvailable, maxBufferSize);
65 buffer = new byte[bufferSize];
66 bytesRead = fileInputStream.read(buffer, 0, bufferSize);
67 while (bytesRead > 0)
68 {
69 dos.write(buffer, 0, bufferSize);
70 bytesAvailable = fileInputStream.available();
71 bufferSize = Math.min(bytesAvailable, maxBufferSize);
72 bytesRead = fileInputStream.read(buffer, 0, bufferSize);
73 }
74 dos.writeBytes(lineEnd);
75 dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
76 BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
77 String inputLine;
78 while ((inputLine = in.readLine()) != null)
79 // close streams
80 Log.e("MediaPlayer","File is written");
81 fileInputStream.close();
82 dos.flush();
83 dos.close();
84 }
85 catch (MalformedURLException ex)
86 {
87 Log.e("Log1", "error: " + ex.getMessage(), ex);
88 }
89 catch (IOException ioe)
90 {
91 Log.e("Log2", "error: " + ioe.getMessage(), ioe);
92 }
93 //
94 }
95 }
96
97
98 }
1public String getPath(Uri uri) {
2
3 String[] projection = { MediaStore.Images.Media.DATA };
4
5 Cursor cursor = managedQuery(uri, projection, null, null, null);
6
7 int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
8
9 cursor.moveToFirst();
10
11 return cursor.getString(column_index);
12
13 }

Antworten
San Blarnoi
  • Forum-Beiträge: 2.545

15.09.2013, 21:33:24 via Website

Ist das gleiche wie in zahllosen anderen Threads hier im Forum: NetworkOnMainThread...

Antworten
Patrick Gallien
  • Forum-Beiträge: 2

15.09.2013, 21:36:08 via App

seit ihr programmierer

Antworten
Kinq__#
  • Forum-Beiträge: 47

15.09.2013, 22:06:25 via Website

and dev
Ist das gleiche wie in zahllosen anderen Threads hier im Forum: NetworkOnMainThread...
Danke für dein Hinweis :*)
Und schon klapt das einwandfrei

mfg

Antworten