Reputation:
My xml file is as shown below, when i select image the cursor select the image but the image is not appearing in my image view.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingBottom="16dp"
android:paddingTop="16dp"
tools:context="com.appsound.instagram.memeappgenerator.MainActivity">
<EditText
android:id="@+id/editTop"
android:layout_width="match_parent"
android:layout_height="35dp"
android:hint="@string/entertopmemetext" />
<EditText
android:layout_width="match_parent"
android:layout_height="35dp"
android:hint="@string/enterbottommemetext"
android:id="@+id/editBottom"/>
<Button
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="@string/add_image"
android:textSize="20sp"
android:background="#F44336"
android:textColor="#000"
android:onClick="addImage"
/>
<Button
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="TRY"
android:textSize="20dp"
android:background="#E1F5"
android:textColor="#FFFFFF"
android:onClick="trymeme" />
<RelativeLayout
android:id="@+id/lay"
android:layout_width="match_parent"
android:layout_height="250dp">
<ImageView
android:id="@+id/memeImage"
android:layout_width="match_parent"
android:layout_height="250dp"
android:src="@mipmap/ic_launcher"
android:visibility="visible" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/meme_top_text"
android:layout_centerHorizontal="true"
android:textColor="#FFFFFF"
android:id="@+id/memeTopText"
android:textAlignment="center"
android:textSize="30dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/memeBottomText"
android:layout_centerHorizontal="true"
android:text="@string/meme_bottom_text"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="30dp"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
<Button
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="@string/save"
android:textSize="20sp"
android:background="#F44336"
android:textColor="#000"
android:onClick="saveImage"/>
</LinearLayout>
my java code is as shown below.
package com.appsound.instagram.memeappgenerator;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView topText;
TextView bottomText;
EditText editTop;
EditText editBottom;
ImageView imageView;
private static int RESULT_LOAD_IMAGE=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
topText=(TextView)findViewById(R.id.memeTopText);
bottomText=(TextView)findViewById(R.id.memeBottomText);
editTop=(EditText)findViewById(R.id.editTop);
editBottom=(EditText)findViewById(R.id.editBottom);
imageView=(ImageView)findViewById(R.id.memeImage);
}
public void addImage(View view){
Intent intent=new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent,RESULT_LOAD_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==RESULT_LOAD_IMAGE && requestCode==RESULT_OK && null!=data){
Uri selectedImage= data.getData();
String[] filepathcolumn={MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,filepathcolumn,null,null,null);
assert cursor != null;
cursor.moveToFirst();
int columnIndex=cursor.getColumnIndex(filepathcolumn[0]);
String picturePath= cursor.getString(columnIndex);
cursor.close();
ImageView imageView=(ImageView) findViewById(R.id.memeImage);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
public void trymeme(View view){
topText.setText(editTop.getText().toString());
bottomText.setText(editBottom.getText().toString());
hideKeyboard(view);
}
public void hideKeyboard(View view){
InputMethodManager imm=(InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),0);
}
}
have a great problem on the image upload on the Image view, the image upload button is working properly and fetches the image but the image is not able to be shown on the image view am stack. please help me i shall be very glad.Thank you
Upvotes: 0
Views: 59
Reputation: 595
Try this fro OnActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
imgView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
Upvotes: 2