我发现我的应用程序的功能不适用于某些Android手机。
我想也许是Android版的问题?我的应用在其他Android手机上工作正常,那么我该如何解决这个问题?
public class StartActivity extends AppCompatActivity {
private Button btnupload,btnchoose;
private ImageView imgprofile;
Uri filepath;
private static final int PICK_IMAGE_REQUEST = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
btnchoose = (Button)findViewById(R.id.btn_addphoto);
btnupload = (Button)findViewById(R.id.btn_upload);
imgprofile = (ImageView)findViewById(R.id.imageView);
btnchoose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
chooseimage();
}
});
btnupload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
}
private void chooseimage() {
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK) {
filepath = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),filepath);
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1) {
Toast.makeText(StartActivity.this, "YES!", Toast.LENGTH_SHORT).show();
bitmap = Bitmap.createScaledBitmap(bitmap, imgprofile.getWidth(),imgprofile.getHeight(),true);
imgprofile.setImageBitmap(bitmap);
} else {
Toast.makeText(StartActivity.this, "YES!!!!", Toast.LENGTH_SHORT).show();
imgprofile.setImageBitmap(bitmap);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
我在5.0.2版本的手机上运行我的应用程序,它没有显示吐司的任何响应。
答案 0 :(得分:0)
<强> MyActivity.xml 强>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/imageView"
android:layout_x="13dp"
android:layout_y="112dp" />
<强> MainActivity.java 强>
public class MainActivityextends Activity {
Button imageUpload;
ImageView imageView;
static final int PICTURE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView= (ImageView) findViewById(R.id.imageView);
imageUpload= (Button) findViewById(R.id.buttonImage);
imageUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent,PICTURE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==PICTURE && resultCode==RESULT_OK && null !=data)
{
Uri uri = data.getData();
String[] prjection ={MediaStore.Images.Media.DATA};
Cursor cursor=getContentResolver().query(uri,prjection,null,null,null);
cursor.moveToFirst();
int columnIndex=cursor.getColumnIndex(prjection[0]);
String path=cursor.getString(columnIndex);
cursor.close();
Bitmap selectFile = BitmapFactory.decodeFile(path);
Drawable d = new BitmapDrawable(selectFile);
imageView.setBackground(d);
// imageView.setImageBitmap(BitmapFactory.decodeFile(path));
}
}
}
检查从图库和显示文件获取图像的简单代码,这可能会对您有所帮助。 Example
答案 1 :(得分:0)
这是代码适合我。我只是改变了函数的位置,代码仍然相同。
package com.example.user8.letseat;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.OnProgressListener;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import java.io.IOException;
import java.util.UUID;
public class StartActivity extends AppCompatActivity {
private Button btnupload,btnchoose;
private ImageView imgprofile;
Uri filepath;
private static final int PICK_IMAGE_REQUEST = 1;
FirebaseStorage firebaseStorage;
StorageReference storageReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
firebaseStorage = FirebaseStorage.getInstance();
storageReference = firebaseStorage.getReference();
btnchoose = (Button)findViewById(R.id.btn_addphoto);
btnupload = (Button)findViewById(R.id.btn_upload);
imgprofile = (ImageView)findViewById(R.id.imageView);
btnchoose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
}
});
btnupload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
uploadimage();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK)
{
filepath = data.getData();
try
{
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),filepath);
imgprofile.setImageBitmap(bitmap);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
private void uploadimage() {
if (filepath != null)
{
final ProgressDialog pg = new ProgressDialog(this);
pg.setTitle("Uploading...");
pg.show();
StorageReference ref = storageReference.child("image/" + UUID.randomUUID().toString());
ref.putFile(filepath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
pg.dismiss();
DatabaseReference databaseReference ;
databaseReference= FirebaseDatabase.getInstance().getReference("Imageeee");
String ImageUploadId = databaseReference.push().getKey();
String imageurl = taskSnapshot.getDownloadUrl().toString();
// Adding image upload id s child element into databaseReference.
databaseReference.child(ImageUploadId).setValue(imageurl);
Toast.makeText(StartActivity.this, "FINISH", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
pg.dismiss();
Toast.makeText(StartActivity.this, "FAIL"+e.getMessage(), Toast.LENGTH_SHORT).show();
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0*taskSnapshot.getBytesTransferred()/taskSnapshot.getTotalByteCount());
pg.setMessage("Uploaded" + (int)progress+"%");
}
});
}
}
}