我已经苦苦挣扎了好几天,在imageview上显示所选图片,我只需要当用户选择或拍照时,图片应显示在我片段内的imageview中,因为现在没有任何反应并且由于某种原因,android studio将我的imageview作为circleimageview取出,不知道这是否可能是问题,我在这里遇到了Imageview问题而且我找不到与我的问题有任何关系因为我想避免问某事这已经得到了回答,如果有请原谅我,但我真的需要帮助,我被困住了,谢谢你提前嘿 - )。
这是我的片段xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/enquirefragment"
tools:context="com.infamuspips.cess.enquire_Fragment">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginTop="0dp"
android:gravity="center"
android:layout_centerHorizontal="true"
android:padding="0dp"
android:src="@drawable/app_icon"
android:id="@+id/app_icon"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Community "
android:id="@+id/app_title"
android:textColor="#000"
android:textSize="18dp"
android:layout_below="@+id/editText3"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Engagement Surveilance "
android:id="@+id/app_title1"
android:layout_centerHorizontal="true"
android:textColor="#000"
android:layout_marginTop="5dp"
android:textSize="18dp"
android:layout_below="@+id/app_title"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please Log your question! "
android:id="@+id/register"
android:textColor="?attr/colorPrimary"
android:layout_marginTop="10dp"
android:textSize="15dp"
android:layout_below="@+id/app_title1"
/>
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:padding="5dp"
android:id="@+id/imageView"
android:background="#FFF"
android:layout_alignParentTop="false"
android:layout_centerVertical="true"
android:layout_alignRight="@+id/app_title1"
android:layout_alignEnd="@+id/app_title1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click to use image for questioning"
android:layout_centerHorizontal="true"
android:textColor="?attr/colorPrimary"
android:textStyle="italic"
android:textSize="15dp"
android:clickable="true"
android:layout="@+id/imageView"
android:id="@+id/questioning"
android:layout_below="@+id/imageView"
android:layout_alignLeft="@+id/imageView"
android:layout_alignRight="@+id/app_title1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText1"
android:enabled="false"
android:layout_above="@+id/logIn"
android:layout_marginTop="25dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editText3"
android:layout_marginTop="29dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/editText4"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="35dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/enquire"
android:layout_marginTop="10dp"
android:layout_below="@+id/questioning"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="@+id/editText4" />
<AutoCompleteTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/autocomplete"
android:ems="10"
android:hint = "module name"
android:lines="1"
android:singleLine="true"
android:completionThreshold="1"
android:layout_below="@+id/app_title1"
android:layout_toEndOf="@+id/app_title1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="@+id/app_title1" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="@+id/btnSubmit"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="@+id/enquire"
android:layout_alignLeft="@+id/autocomplete"
android:layout_alignStart="@+id/autocomplete" />
</RelativeLayout>
这是我的Fragment类中的方法
public class enquire_Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
RelativeLayout enquire = (RelativeLayout) inflater.inflate(R.layout.fragment_enquire_, container, false);
return Myview;
}
public void onActivityCreated (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//Run Background Activity
Module = (AutoCompleteTextView) getActivity().findViewById(R.id.autocomplete);
GetModules getmodules = new GetModules();
getmodules.execute();
//Find ChildObjects
up = (EditText) getActivity().findViewById(R.id.editText3);
down = (EditText) getActivity().findViewById(R.id.editText4);
enquire = (EditText) getActivity().findViewById(R.id.enquire);
que = (TextView) getActivity().findViewById(R.id.questioning);
imageView = (ImageView) getActivity().findViewById(R.id.imageView);
button = (Button) getActivity().findViewById(R.id.btnSubmit);
//Set attributes
up.setEnabled(false);
down.setEnabled(false);
que.setClickable(true);
button.setEnabled(false);
Module.requestFocus();
Module.addTextChangedListener(textwatcher);
enquire.addTextChangedListener(textwatcher);
//Call operations
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
que.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectImage();
}
});
}
private void selectImage() {
final CharSequence[] options = { "Take Photo", "Choose from Gallery","Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Add Photo!");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo"))
{
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 (options[item].equals("Choose from Gallery"))
{
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(intent, "Select File"),2);
}
else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
//@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == getActivity().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);
imageView.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 el) {
el.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == 2) {
Uri selectedImage = data.getData();
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor c = getActivity().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(getActivity()"path of image from gallery......******************.........", picturePath+"");
((BitmapDrawable)imageView.getDrawable()).getBitmap().recycle();
imageView.setImageBitmap(thumbnail);
}
}
}
这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.infamuspips.cess">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".registration"
android:label="@string/app_name"></activity>
<activity
android:name=".reg_name"
android:label="@string/app_name"></activity>
<activity
android:name=".reg_securityquestions"
android:label="@string/app_name"></activity>
<activity
android:name=".reg_id"
android:label="@string/app_name"></activity>
<activity
android:name=".reg_cellnumber"
android:label="@string/app_name"></activity>
<activity
android:name=".reg_email"
android:label="@string/app_name"></activity>
<activity
android:name=".reg_password"
android:label="@string/app_name"></activity>
<activity
android:name=".Maindrawer"
android:label="@string/title_activity_maindrawer"
android:theme="@style/AppTheme"></activity>
</application>
</manifest>
这是我的gradle文件 -
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.infamuspips.cess"
minSdkVersion 14
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:design:22.2.1'
compile 'de.hdodenhof:circleimageview:1.3.0'
compile 'io.realm:realm-android:0.84.1'
compile 'com.android.support:support-v4:22.2.1'
}