-2

I have develop code on android to upload the image, but facing some errors in code. I search out solutions on internet but could not successful to fix the problems? The following code I have used:


import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.net.Uri;




public class MainActivity extends AppCompatActivity {
    private static final int SELECTED_PICTURE=1;
    ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv=(ImageView)findViewById(R.id.imageView1);
    }
    public void btnClick(View v){
        Intent i=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i,SELECTED_PICTURE);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode,resultCode, data);
        switch (requestCode)
        {
            case SELECTED_PICTURE:
                if(resultCode==RESULT_OK)
                {
                    Uri uri=data.getData();
                    String[]projection={MediaStore.Images.Media.DATA};
                    Cursor cursor=getContentResolver().query(uri, projection, null, null, null);
                    cursor.moveToFirst();
                    int ColumnIndex= cursor.getColumnIndex(projection[0]);
                    String filePath=cursor.getString(ColumnIndex);
                    cursor.close();
                    Bitmap YourSelectedImage=BitmapFactory.decodeFile(filePath);
                    Drawable d=new BitmapDrawable(YourSelectedImage);
                    iv.setBackground(d);
                }
                break;
            ault:
           break;
        }
    }
}

In line 49 (iv.setBackground(d);) there is an error of “call require API 16 android.view.View#setBackground. In line 52 (ault:) there is an error of unused label ault. In line 53 (break;) there is an error of unreachable statement.

Please help me to get out this problems?

Kabir
  • 852
  • 7
  • 11
salma khalil
  • 147
  • 1
  • 8

1 Answers1

1

You can use :

iv.setBackgroundDrawable(d);

Explanation for why it's showing error :

int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    setBackgroundDrawable();
} else {
    setBackground();
}
Ashish
  • 6,791
  • 3
  • 26
  • 48